QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#406657#5651. Parmigiana With SeafoodJryno1WA 56ms27396kbC++141.1kb2024-05-07 16:10:022024-05-07 16:10:04

Judging History

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

  • [2024-05-07 16:10:04]
  • 评测
  • 测评结果:WA
  • 用时:56ms
  • 内存:27396kb
  • [2024-05-07 16:10:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int n,deg[maxn],dep[maxn],cnt[maxn],vis[maxn],fa[maxn];
vector<int>ed[maxn];
int ans=0;
void dfs(int x,int fat,int dis){
    fa[x]=fat;
    if(dis&1)ans=max(ans,x);
    dep[x]=dis;
    for(auto v:ed[x]){
        if(v==fat)continue;
        dfs(v,x,dis+1);
    }
}
int main(){
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        ed[u].push_back(v);
        ed[v].push_back(u);
        deg[u]++,deg[v]++;
    }
    if(n&1){
        // for(int i=1;i<=n;i++)if(deg[i]==1)ans=max(ans,i);
        dfs(n,0,0);
        vis[n]=1;
        for(int i=n;i>=1;i--){
            if(dep[i]&1){
                cout<<i<<"\n";
                return 0;
            }
            int now=i;
            while(!vis[now]){
                vis[now]=1;
                cnt[now]++,cnt[fa[now]]++,now=fa[now];
            }
            if(cnt[now]>2&&!(dep[now]&1)){
                cout<<i<<"\n";
                return 0;
            }
        }
    } else cout<<n<<"\n";
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 19376kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 4ms
memory: 22744kb

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 41ms
memory: 25300kb

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:

99925

result:

ok single line: '99925'

Test #4:

score: -100
Wrong Answer
time: 56ms
memory: 27396kb

input:

99997
90325 59106
22545 8765
88871 37709
14739 95233
8778 29659
48110 57549
91258 76066
15724 65144
48244 87291
12076 94378
41946 96707
93645 12812
53817 34343
72097 94062
81212 263
78713 78150
6754 94906
20957 97539
59293 5018
77961 78090
57262 95225
79349 47902
99024 7869
10613 13728
61757 41090
4...

output:

85397

result:

wrong answer 1st lines differ - expected: '85398', found: '85397'