QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#597253 | #7755. Game on a Forest | Your-Sun# | WA | 2ms | 5460kb | C++20 | 1.2kb | 2024-09-28 17:22:34 | 2024-09-28 17:22:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
vector<int> fa,sz;
int find(int x) // 找根节点
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void uni(int x, int y) // 合并并查集
{
x=find(x),y=find(y);
if(x==y) return;
if(sz[x]>sz[y]) swap(x,y);
fa[x]=y;
sz[y]+=sz[x];
}
void solve() {
int n,m;
cin>>n>>m;
fa.resize(n+1), sz.resize(n+1,1);
iota(fa.begin(),fa.end(),0);
int i,j,k,u,v;
for(i=0;i<m;i++)
{
cin>>u>>v;
uni(u,v);
}
int cnt=0;
map<int,int> mp;
vector<int> vis(n+1);
for(i=1;i<=n;i++)
{
int t=find(i);
if(vis[t])
continue;
cnt++;
vis[t]=1;
t=sz[t];
if(t>=3) mp[3]++, k=t;
if(t==2) mp[2]++;
}
if(cnt==1)
{
cout<<n-1<<endl;
return;
}
// cerr<<"end\n";
if(mp[3]==0)
cout<<2*mp[2]<<endl;
else if(mp[3]==1)
cout<<2*mp[2]<<endl;
else
cout<<0<<endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
4 3 1 2 2 3 3 4
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 5460kb
input:
100000 1 4647 17816
output:
2
result:
wrong answer 1st numbers differ - expected: '1', found: '2'