QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358599#6307. Chase Game 2Djangle162857#WA 10ms12000kbC++202.1kb2024-03-19 21:22:492024-03-19 21:22:49

Judging History

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

  • [2024-03-19 21:22:49]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:12000kb
  • [2024-03-19 21:22:49]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

using namespace std;
const int N=200010;
int n,cnt,c[N];


struct Graph {
    int tot,root,ind[N],head[N],suiv[N<<1],ver[N<<1],dis[N<<1],
        fa[N<<1];

    inline void init()
    {
        for(int i=0; i<=tot; i++)
            head[i]=dis[i]=ind[i]=fa[i]=0;
        root=tot=0;
    }

    inline void lnk(int x,int y)
    {
        ver[++tot]=y;
        suiv[tot]=head[x];
        head[x]=tot;
        ind[y]++;
    }

    inline void dfs(int x)
    {
        //    cout << "dfs " << x << endl;
        dis[x]=dis[fa[x]]+1;
        if(dis[x]>dis[root])
            root=x;
        for(int i=head[x]; i; i=suiv[i]) {
            int y=ver[i];
            if(y==fa[x])
                continue;
            fa[y]=x;
            dfs(y);
        }
    }

} e;


inline void sol()
{
    cnt=0;
    e.init();
    scanf("%d",&n);
    for(int i=1,x,y; i<n; i++)
        scanf("%d%d",&x,&y),e.lnk(x,y),e.lnk(y,x);

    int ind_cnt=0;
    for(int i=1; i<=n; i++) {
        if(e.ind[i]>1)
            ind_cnt++;
    }

    if(ind_cnt<=1) {
        puts("-1");
        return;
    }

    e.dfs(1);
    e.dis[e.root]=1;
    e.fa[e.root]=0;
    e.dfs(e.root);
    for(int i=e.root; i; i=e.fa[i]) {
        c[++cnt]=i;
    }
    // for (int i = 1; i <= cnt; i++)
    //    cout << c[i] << endl;
    // cout << endl;

    ind_cnt=0;
    int leaf=0,ans=0,maxiind=0;
    for(int i=1;i<=n;i++)
        if(e.ind[i]==1)leaf++;

    for(int i=2; i<cnt; i++) {
        maxiind=max(maxiind,e.ind[c[i]]);
        if(e.ind[c[i]]>2)
            ind_cnt++;
    }
    if(!ind_cnt)
        ans=leaf/2;
    else if(ind_cnt==1)
    {
        if(e.ind[c[2]]==maxiind||e.ind[c[cnt-1]]==maxiind)
            ans=maxiind-1;
        else ans=(leaf+1)>>1;
    }
    else ans=(leaf+1)>>1;
    printf("%d\n",ans);
}


signed main()
{
    int T;
    scanf("%d",&T);
    while(T--)
        sol();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11996kb

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: 0
Accepted
time: 3ms
memory: 9960kb

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
2
2
1
2
-1
2
2
1
2
2
1
1
1
-1
2
2
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
2
1
1
2
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
2
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
2
1
1
1
1
-1
2
1
2
1
1
1
1
1
1
1
2
-1...

result:

ok 10000 numbers

Test #3:

score: -100
Wrong Answer
time: 10ms
memory: 12000kb

input:

10000
9
1 2
2 3
3 4
4 5
1 6
6 7
5 8
7 9
9
1 2
2 3
2 4
1 5
2 6
4 7
6 8
1 9
9
1 2
2 3
1 4
4 5
5 6
4 7
2 8
1 9
10
1 2
2 3
1 4
3 5
3 6
2 7
6 8
6 9
6 10
10
1 2
1 3
3 4
2 5
1 6
5 7
4 8
2 9
7 10
10
1 2
2 3
2 4
1 5
3 6
6 7
5 8
4 9
9 10
9
1 2
2 3
2 4
1 5
3 6
2 7
1 8
2 9
9
1 2
1 3
2 4
1 5
3 6
3 7
7 8
8 9
10
1...

output:

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

result:

wrong answer 37th numbers differ - expected: '4', found: '3'