QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#251031#852. Jellyfishthomas_li#WA 158ms3616kbC++171.2kb2023-11-14 05:09:512023-11-14 05:09:52

Judging History

This is the latest submission verdict.

  • [2023-11-14 05:09:52]
  • Judged
  • Verdict: WA
  • Time: 158ms
  • Memory: 3616kb
  • [2023-11-14 05:09:51]
  • 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,circ = 0;
	rep(i,0,n) if(sz(adj[i]) == 1) leaves++;
	vi vis(n),stk,on(n);
	auto dfs = [&](auto& dfs, int u)->void{
		vis[u] = 1; stk.PB(u);
		for(int v : adj[u]){
			if(!vis[v]) dfs(dfs,v);
			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);
	rep(i,0,n){
		if(on[i] && sz(adj[i]) == 2) circ++;
	}
	cout << max(3ll,leaves+min(2ll,circ)) << "\n";
}
signed main(){
	cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
	int t; cin >> t; while(t--) solve();
}


详细

Test #1:

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

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
Wrong Answer
time: 158ms
memory: 3616kb

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
4
5
4
5
4
5
4
4
3
4
5
4
4
4
4
4
4
5
4
4
3
5
3
9
4
5
3
4
8
4
99
5
4
3
7
5
4
5
5
4
5
4
4
4
5
3
5
5
3
4
97
4
4
4
5
4
3
4
3
6
4
3
4
3
3
4
4
4
4
4
4
4
4
5
3
3
3
4
5
4
4
4
4
5
5
5
4
4
5
5
4
5
5
3
4
4
3
3
4
5
4
4
4
6
4
5
5
5
4
3
5
5
4
3
5
10
5
4
3
4
4
4
5
5
4
4
5
4
4
4
3
3
4
4
5
99
5
106
5
4
4
4
5
...

result:

wrong answer 5th numbers differ - expected: '3', found: '4'