QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#874465#852. Jellyfishasdfsdf#TL 2ms10052kbC++231.7kb2025-01-28 06:21:452025-01-28 06:21:46

Judging History

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

  • [2025-01-28 06:21:46]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:10052kb
  • [2025-01-28 06:21:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<int, int>> st;
#define MAX 1010101
vector<int> adj[MAX];
int p[MAX];
int find(int a) {
	if (p[a] == a) return a;
	return p[a] = find(p[a]);
}
bool uni(int a, int b) {
	a = find(a);
	b = find(b);
	if (a == b) return false;
	p[a] = b;
	return true;
}
vector<int> path;
bool dfs(int x, int e, int p = 0) {
	path.push_back(x);
	if (x == e) return true;
	for (auto v : adj[x]) if (v != p && dfs(v, e, x)) return true;
	path.pop_back();
	return false;
}
int chk[MAX];
int dp[MAX];
int sz[MAX];
void calc(int x, int p = 0) {
	dp[x] = 0;
	for (auto v : adj[x]) {
		if (~chk[v]) continue;
		if (v == p) continue;
		sz[x] = 1;
		calc(v, x);
		dp[x] += dp[v];
	}
	if (!sz[x]) dp[x] = 1;
}
void solve() {
	int N;
	cin >> N;
	int i, a, b;
	int s, t;
	s = t = 0;
	path.clear();
	for (i = 1; i <= N; i++) p[i] = i, chk[i] = dp[i] = sz[i] = 0, adj[i].clear();
	for (i = 1; i <= N; i++) {
		cin >> a >> b;
		if (uni(a, b)) {
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		else s = a, t = b;
	}
	assert(s && t);
	dfs(s, t);
	int M = path.size();
	memset(chk, -1, sizeof(chk));
	for (i = 0; i < M; i++) chk[path[i]] = i;
	for (auto v : path) calc(v);
	int ans = 3;
	int dpsum = 0;
	int scnt = 0;
	for (auto v : path) {
		if (sz[v]) dpsum += dp[v];
		else scnt++;
	}
	for (i = 0; i < M; i++) {
		int c = path[i];
		int n = path[(i + 1) % M];
		int res = dpsum + 2;
		if (sz[c]) res -= dp[c];
		if (sz[n]) res -= dp[n];
		ans = max(ans, res);
	}
	ans = max(ans, dpsum + (scnt > 0));
	cout << ans << '\n';
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	cin >> T;
	while (T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10052kb

input:

2
6
1 2
2 3
3 4
4 1
2 5
2 6
4
1 2
2 3
3 4
4 1

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: -100
Time Limit Exceeded

input:

85665
6
3 2
4 1
4 6
2 1
2 6
5 1
7
6 2
6 3
5 1
1 2
2 4
4 7
3 7
7
6 1
6 7
1 4
1 3
7 5
5 3
4 2
7
6 2
7 4
7 5
3 1
3 4
2 5
1 4
7
7 2
2 6
5 4
5 6
5 1
3 1
4 6
7
3 5
3 1
3 7
3 2
5 1
5 4
4 6
7
4 5
4 1
3 6
3 7
6 7
6 1
2 1
7
5 3
7 3
1 4
6 2
6 3
2 3
4 3
7
2 3
2 6
2 4
7 5
3 5
5 1
1 4
7
3 4
3 7
5 6
2 7
4 6
6 7
6 ...

output:

4
3
3
3
3
4
4
5
4
5
4
4
3
4
4
3
4
4
4
4
4
5
3
4
3
4
3
9
4
4
3
4
8
3
98
5
4
3
6
4
4
4
4
3
4
4
4
4
5
3
5
4
3
4
95
4
4
4
5
4
3
4
3
5
4
3
4
3
3
4
4
4
4
4
3
4
4
4
3
3
3
4
4
3
4
4
4
4
4
4
3
3
5
5
4
5
4
3
4
4
3
3
3
5
4
4
4
6
4
5
5
5
4
3
5
4
4
3
4
10
4
3
3
4
4
3
5
4
4
3
5
3
4
4
3
3
3
4
5
98
5
105
4
4
4
3
4
...

result: