QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#596654 | #7755. Game on a Forest | Your-Sun# | WA | 2ms | 5412kb | C++20 | 1.1kb | 2024-09-28 16:11:55 | 2024-09-28 16:11:58 |
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);
}
map<int,int> mp;
vector<int> vis(n+1);
for(i=1;i<=n;i++)
{
int t=find(i);
if(vis[t])
continue;
vis[t]=1;
t=sz[t];
if(t>=3) mp[3]++, k=t;
if(t==2) mp[2]++;
}
cerr<<"end\n";
if(mp[3]==0)
cout<<2*mp[2]<<endl;
else if(mp[3]==1)
cout<<2*mp[2]+k-1<<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: 3636kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
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: 5412kb
input:
100000 1 4647 17816
output:
2
result:
wrong answer 1st numbers differ - expected: '1', found: '2'