QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#657474#6307. Chase Game 2Fluoresce#WA 4ms5756kbC++201.7kb2024-10-19 14:49:452024-10-19 14:49:45

Judging History

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

  • [2024-10-19 14:49:45]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5756kb
  • [2024-10-19 14:49:45]
  • 提交

answer

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
typedef long long ll;
typedef long double ld;
#define debug(a) cout<<a<<'\n'
#define Pll pair<ll,ll>
#define PII pair<int,int>
#define ft first
#define sd second
#define vec vector
#define pushk push_back
#define ump unordered_map
#define pl p<<1
#define pr p<<1|1
using namespace std;
const int N = 2e5 + 10, M = 1e4 + 10, mod = 1e9 + 7;
const ll inf = 1e18;
const ld eps = 1e-13;
int mov[4][2] = { {0,1},{1,0},{-1,0},{0,-1} }, dx, dy, _ = 1, __ = 1;
void bout(bool f) {
	if (f)cout << "Yes\n";
	else cout << "No\n";
}
ll n, m, k;
int h[N],e[N<<1],ne[N<<1],idx;

void link(int x,int y){
	e[++idx]=y;
	ne[idx]=h[x];
	h[x]=idx;
}
map<int,int>mp;
int sum=0,cnt=0,dep;
bool vis[N];

bool dfs(int u,int dp){
	vis[u]=1;
	int d=0;
	bool f=1;
	for(int i=h[u];~i;i=ne[i]){
		int v=e[i];
		if(!vis[v]){
			if(dfs(v,dp+1))
			++d;
			f=0;
		}
	}
	vis[u]=0;
	if(d){
		++cnt;
		++mp[d];
	}
	sum+=d;
	dep=max(dep,dp);
	return f;
}
void ini() {
}
void solve() {
	cin>>n;
	memset(h,-1,4*(n+3));idx=0;
	mp.clear();
	sum=0;cnt=0;
	int x,y;
	for(int i=1;i<n;++i){
		cin>>x>>y;
		link(x,y);
		link(y,x);
	}
	dep=1;
	dfs(1,1);
	if(cnt>1||dep>3)cout<<max((*mp.rbegin()).ft,(sum+1)/2)<<'\n';
	else cout<<"-1\n";
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	streambuf* cinbackup = cin.rdbuf(), * coutbackup = cout.rdbuf();
	ifstream fin("in.txt");
	ofstream fout("out.txt");
	cin.rdbuf(fin.rdbuf());
	cout.rdbuf(fout.rdbuf());
#endif
	cin >> _;
	__ = _;
	ini();
	while (_--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

-1
1
-1
2

result:

ok 4 number(s): "-1 1 -1 2"

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 5696kb

input:

10000
4
1 2
1 3
3 4
4
1 2
1 3
1 4
4
1 2
2 3
1 4
5
1 2
2 3
1 4
4 5
5
1 2
2 3
3 4
4 5
4
1 2
2 3
2 4
5
1 2
1 3
2 4
2 5
4
1 2
2 3
1 4
5
1 2
1 3
2 4
1 5
5
1 2
2 3
3 4
2 5
5
1 2
1 3
2 4
2 5
4
1 2
1 3
3 4
5
1 2
1 3
3 4
1 5
4
1 2
1 3
1 4
5
1 2
1 3
3 4
3 5
5
1 2
2 3
3 4
3 5
4
1 2
1 3
2 4
5
1 2
2 3
2 4
3 5
5
...

output:

1
-1
1
1
1
-1
2
1
2
1
2
1
2
-1
2
2
1
1
2
1
1
1
-1
2
1
2
1
-1
1
1
2
1
1
-1
1
2
1
1
1
-1
1
1
2
2
2
1
1
1
-1
1
1
1
1
1
1
2
1
1
2
-1
-1
-1
2
2
2
1
1
1
2
2
2
-1
1
2
-1
1
1
-1
2
-1
-1
1
2
2
1
1
1
1
1
1
1
1
1
1
2
-1
1
1
2
-1
2
1
1
1
-1
2
-1
1
-1
-1
2
-1
2
1
2
2
1
1
1
1
1
1
1
1
1
-1
1
1
2
1
1
1
1
1
1
1
2
-1...

result:

wrong answer 10th numbers differ - expected: '2', found: '1'