QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#107692#5379. Adjoin the NetworksPetroTarnavskyi#WA 3ms6532kbC++171.2kb2023-05-22 15:30:312023-05-22 15:30:32

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-22 15:30:32]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:6532kb
  • [2023-05-22 15:30:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int N = 1 << 17;

vector<int> g[N];
bool used[N];
int maxD, maxV;

void dfs(int v, int p, int d) {
	used[v] = true;
	if (d > maxD) {
		maxD = d;
		maxV = v;
	}
	for (int to : g[v]) {
		if (to != p) {
			dfs(to, v, d + 1);
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	while (m--) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> w;
	FOR(i, 0, n) {
		if (used[i]) {
			continue;
		}
		maxD = -1;
		dfs(i, -1, 0);
		maxD = -1;
		dfs(maxV, -1, 0);
		w.push_back((maxD + 1) / 2);
	}
	sort(ALL(w));
	int ans = w.back();
	if (SZ(w) >= 2) {
		ans = max(ans, w.back() + 1 + w[SZ(w) - 2]);
	}
	if (SZ(w) >= 3) {
		ans = max(ans, w[SZ(w) - 2] + 2 + w[SZ(w) - 3]);
	}
	cout << ans << "\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6468kb

input:

6 4
0 1
0 2
3 4
3 5

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 6432kb

input:

11 9
0 1
0 3
0 4
1 2
5 4
6 4
7 8
7 9
7 10

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 3ms
memory: 6532kb

input:

5 0

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 3ms
memory: 6428kb

input:

6 3
0 1
2 3
4 5

output:

4

result:

ok single line: '4'

Test #5:

score: 0
Accepted
time: 3ms
memory: 6528kb

input:

21 6
0 1
1 2
2 3
4 5
5 6
6 7

output:

5

result:

ok single line: '5'

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 6432kb

input:

12 6
0 1
1 2
2 3
3 4
4 5
5 6

output:

4

result:

wrong answer 1st lines differ - expected: '6', found: '4'