QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#596654#7755. Game on a ForestYour-Sun#WA 2ms5412kbC++201.1kb2024-09-28 16:11:552024-09-28 16:11:58

Judging History

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

  • [2024-09-28 16:11:58]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5412kb
  • [2024-09-28 16:11:55]
  • 提交

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'