QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84384#5651. Parmigiana With SeafoodPdw2007WA 13ms6032kbC++141.0kb2023-03-06 14:12:522023-03-06 14:12:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 14:12:53]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:6032kb
  • [2023-03-06 14:12:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,ans;
int tot,head[N];
struct edge{
    int to,nxt;
}e[N<<1];
int d[N],mx;
void add(int x,int y) {
    tot++;
    e[tot].to=y;e[tot].nxt=head[x];head[x]=tot;
    return;
}
int mx1,mx2,mx3;
void dfs(int x,int fa) {
    d[x]=d[fa]+1;
    if(d[x]&1) mx=max(mx,x);
    else if(x!=n) {
        if(x>mx1) mx3=mx2,mx2=mx1,mx1=x;
        else if(x>mx2) mx3=mx2,mx2=x;
        else if(x>mx3) mx3=x;
    }

    int koishi=0;
    for(int i=head[x];i;i=e[i].nxt) {
        int to=e[i].to;
        if(to==fa) continue;
        dfs(to,x);
        koishi++;
    }
    if(!koishi) ans=max(ans,x);
    if(koishi==1&&fa==0) ans=max(ans,x);
    return;
}
int main() {
    scanf("%d",&n);
    for(int i=1,x,y;i<n;i++) {
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    if(!(n&1)) {
        printf("%d",n);
        return 0;
    }
    d[0]=-1;
    dfs(n,0);
    ans=max(ans,mx);
    ans=max(ans,mx3);
    printf("%d\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

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

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: -100
Wrong Answer
time: 13ms
memory: 6032kb

input:

99999
81856 39633
81856 94012
99999 43062
99946 220
81856 46131
99933 36505
99939 35662
99952 70971
99999 3275
99938 58416
99976 66658
99991 87922
81856 80992
99933 6392
99951 41047
99970 54115
81856 38150
99934 73554
81856 64578
81856 18576
99951 67996
99938 84479
81856 39617
99999 18664
99946 2505...

output:

99996

result:

wrong answer 1st lines differ - expected: '99925', found: '99996'