91分第一个点wa了,用了dfs怎么还错了。。。
查看原帖
91分第一个点wa了,用了dfs怎么还错了。。。
675527
Play_CP_4fun楼主2022/8/26 07:44
/*
Author: SJ

general
1.neng bu neng bao li;
2.0 1 zhe liangge tepan
3.dfs jide biaoji qidian

str
1.yongle s.substr(), jide kanyixiashibushikeyi yigeyige shuchu

think twice, code once.

*/
#include <bits/stdc++.h>
const int N = 1e5 + 10;
const int INF = 1e9;
using ll = long long;

int n, x, y, h, d, check[50];
std::vector<int> t[110];
std::queue<int> q;
bool vis[110];
void dfs1(int x, int y) {
	h = std::max(h, y);
	for (auto i : t[x]) {
		if (!vis[i]) {
			vis[i] = true;
			dfs1(i, y + 1);
			vis[i] = false;
		}
	}
}
void dfs2(int j, int k) {
	if (j == y) {
		d = k;
		return;
	}
	for (auto i : t[j]) {
		if (!vis[i]) {
			vis[i] = true;
			if (i > j) dfs2(i, k + 1);
			else dfs2(i, k + 2);
			vis[i] = false;
		}
	}
}
int main() {
	std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    std::cin >> n;
    while (n--) {
    	if (n == 0) {
    		std::cin >> x >> y;
    		break;
    	}

    	int u, v;
    	std::cin >> u >> v;
    	t[u].push_back(v);
    	t[v].push_back(u);
    }
    vis[1] = true;
    dfs1(1, 1);
    std::cout << h << "\n";

    memset(vis, 0, sizeof vis);

    vis[1] = true;
    q.push(1);
    check[1] = 1;
    int cnt1 = 0, cnt2 = 0, w = 0, mark = 2;
    while (!q.empty()) {
    	if (cnt1 == check[mark - 1]) {
    		w = std::max(cnt2, w);
    		check[mark] = cnt2;
    		cnt1 = 1;
    		cnt2 = 0;
    		mark++;
    	} else cnt1++;
    	int p = q.front();
    	q.pop();
    	for (auto i : t[p]) {
    		if (!vis[i]) {
    			vis[i] = true;
    			q.push(i);
    			cnt2++;
    		}
    	}
    }
    std::cout << w << "\n";


    memset(vis, 0, sizeof vis);

    

    vis[x] = true;
    dfs2(x, 0);
    std::cout << d;
	return 0;
}
2022/8/26 07:44
加载中...