QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#287621#7936. Eliminate TreePhantomThreshold#WA 75ms16316kbC++201.2kb2023-12-20 20:34:262023-12-20 20:34:26

Judging History

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

  • [2023-12-20 20:34:26]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:16316kb
  • [2023-12-20 20:34:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<vector<int>> G(n+5);
	vector<int> deg(n+5);
	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		deg[u]++,deg[v]++;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<int> vis(n+5);
	queue<int> q1,q2;
	int ans=0;
	auto check=[&](int u)
	{
		if(vis[u])return;
		for(auto v:G[u])
		{
			if(vis[v])continue;
			if(deg[u]==1 and deg[v]<=2)q2.push(u);
			if(deg[v]==1 and deg[u]<=2)q2.push(u);
		}
		if(deg[u]<=1)q1.push(u);
	};
	for(int i=1;i<=n;i++)
	{
		check(i);
	}
	while(not q1.empty() or not q2.empty())
	{
		while(not q2.empty())
		{
			int u=q2.front();q2.pop();
			if(vis[u] or deg[u]!=1)continue;
			ans++;
			for(auto v:G[u])
			{
				if(not vis[v])
				{
					for(auto t:G[v])
					{
						if(not vis[t])
						{
							deg[t]--,deg[v]--;
							check(t);
						}
					}
					vis[u]=vis[v]=1;
					break;
				}
			}
		}
		if(not q1.empty())
		{
			int u=q1.front();q1.pop();
			if(vis[u])continue;
			vis[u]=1;ans+=2;
			for(auto v:G[u])
			{
				if(not vis[v])
				{
					deg[v]--;
					deg[u]--;
					check(v);
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

詳細信息

Test #1:

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

input:

5
1 2
2 3
3 4
3 5

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 75ms
memory: 16316kb

input:

196666
32025 108673
181779 17115
162570 134230
93003 39673
89144 1269
185919 154408
34896 65369
35860 44720
55698 1390
45520 189805
147867 124511
135459 132555
87303 18031
176314 59695
33352 130640
87272 39716
35749 108807
143493 94486
126512 116598
40212 70895
132216 80855
22241 188737
150354 17346...

output:

167849

result:

wrong answer 1st numbers differ - expected: '138182', found: '167849'