QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251036#852. Jellyfishthomas_li#AC ✓197ms17920kbC++171.3kb2023-11-14 05:24:282023-11-14 05:24:29

Judging History

This is the latest submission verdict.

  • [2023-11-14 05:24:29]
  • Judged
  • Verdict: AC
  • Time: 197ms
  • Memory: 17920kb
  • [2023-11-14 05:24:28]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x,y) x = max(x,y)
#define cmn(x,y) x = min(x,y)
#define ary(k) array<int,k>
typedef pair<int,int> pii;
typedef vector<int> vi;
void solve(){	
	int n; cin >> n;
	vector<vi> adj(n);
	rep(i,0,n){
		int u,v; cin >> u >> v;
		u--; v--;
		adj[u].PB(v); adj[v].PB(u);
	}
	int leaves = 0,cc = 0;
	rep(i,0,n) if(sz(adj[i]) == 1) leaves++;
	vi vis(n),stk,on(n);
	auto dfs = [&](auto& dfs, int u, int p)->void{
		vis[u] = 1; stk.PB(u);
		for(int v : adj[u])if(v != p){
			if(!vis[v]) dfs(dfs,v,u);
			else if(vis[v] == 1){
				for(int i = sz(stk)-1; i >= 0; i--){
					on[stk[i]] = 1;
					if(stk[i] == v) break;
				}
			}
		}
		vis[u] = 2; stk.pop_back();
	};
	dfs(dfs,0,-1);
	vi circ(n);
	rep(i,0,n){
		if(on[i] && sz(adj[i]) == 2){
			circ[i] = 1;
			cc++;
		}
	}
	bool flag = 0;
	rep(u,0,n) for(int v : adj[u]) if(circ[u] && circ[v]) flag = 1;
	int ans = 0;
	if(flag) ans = 2;
	else ans = min(1ll,cc);
	cout << max(3ll,leaves+ans) << "\n";
}
signed main(){
	cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
	int t; cin >> t; while(t--) solve();
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3376kb

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: 0
Accepted
time: 163ms
memory: 3644kb

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:

ok 85665 numbers

Test #3:

score: 0
Accepted
time: 197ms
memory: 17920kb

input:

30
2229
1066 248
248 881
881 2080
2080 1615
1615 1647
1647 1774
1774 196
196 434
434 390
390 129
129 563
563 63
63 1457
1457 1015
1015 2200
2200 1187
1187 1763
1763 1121
1121 2122
2122 1783
1783 1756
1756 2031
2031 2153
2153 605
605 1778
1778 1287
1287 2062
2062 817
817 194
194 474
474 414
414 1736
...

output:

1092
881
722
1412
37556
638
438
509
273
29198
740
27535
46011
865
444
30031
49564
794
489
469
624
956
1180
17384
50000
715
1291
49920
1465
3

result:

ok 30 numbers