QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#114564 | #852. Jellyfish | EastKing# | WA | 87ms | 9648kb | C++17 | 1.7kb | 2023-06-22 16:43:20 | 2023-06-22 16:43:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int n;
int head[M],asdf,du[M];
struct edge{
int to,nxt;
}G[M<<1];
void add_edge(int a,int b){
G[++asdf].to=b;
G[asdf].nxt=head[a];
head[a]=asdf;
}
int par[M];
int get_fa(int x){
if(par[x]==x)return x;
return par[x]=get_fa(par[x]);
}
int fa[M],dep[M];
void dfs(int x,int f){
fa[x]=f;
dep[x]=dep[f]+1;
for(int i=head[x];i;i=G[i].nxt){
int y=G[i].to;
if(y==f)continue;
dfs(y,x);
}
}
int stk1[M],stk2[M];
int calc(int a,int b){
int top1=0,top2=0;
while(a!=b){
if(dep[a]>=dep[b]){
stk1[++top1]=a;
a=fa[a];
}else {
stk2[++top2]=b;
b=fa[b];
}
}
stk1[++top1]=a;
while(top2>0)stk1[++top1]=stk2[top2--];
int len=0;
for(int i=1;i<=top1;i++){
int x=stk1[i];
//printf("x=%d\n",x);
if(du[x]>2){
stk2[++len]=i;
}
}
int ans=0;
for(int i=1;i<=n;i++)ans+=(du[i]==1);
//printf("len=%d\n",len);
if(len==0){
return 3;
}else if(len==1){
return ans+2;
}else {
int mx=0;
stk2[0]=stk2[len]-top1;
for(int i=1;i<=len;i++){
//printf("x=%d\n",stk2[i]);
mx=max(mx,(stk2[i]-stk2[i-1]-1));
}
return ans+mx;
}
}
void solve(){
asdf=0;
for(int i=1;i<=n;i++){
head[i]=0;
du[i]=0;
par[i]=i;
}
int ea=0,eb=0;
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
du[a]++;
du[b]++;
int x=get_fa(a);
int y=get_fa(b);
if(x==y){
ea=a;
eb=b;
}else {
par[x]=y;
add_edge(a,b);
add_edge(b,a);
}
}
dfs(1,0);
cout<<max(calc(ea,eb),3)<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int cas;
cin>>cas;
while(cas--){
cin>>n;
solve();
}
return 0;
}
/*
1
7
1 2
2 3
3 4
4 1
2 7
3 6
4 5
*/
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7488kb
input:
2 6 1 2 2 3 3 4 4 1 2 5 2 6 4 1 2 2 3 3 4 4 1
output:
4 3
result:
ok 2 number(s): "4 3"
Test #2:
score: -100
Wrong Answer
time: 87ms
memory: 9648kb
input:
85665 6 3 2 4 1 4 6 2 1 2 6 5 1 7 6 2 6 3 5 1 1 2 2 4 4 7 3 7 7 6 1 6 7 1 4 1 3 7 5 5 3 4 2 7 6 2 7 4 7 5 3 1 3 4 2 5 1 4 7 7 2 2 6 5 4 5 6 5 1 3 1 4 6 7 3 5 3 1 3 7 3 2 5 1 5 4 4 6 7 4 5 4 1 3 6 3 7 6 7 6 1 2 1 7 5 3 7 3 1 4 6 2 6 3 2 3 4 3 7 2 3 2 6 2 4 7 5 3 5 5 1 1 4 7 3 4 3 7 5 6 2 7 4 6 6 7 6 ...
output:
4 3 3 3 3 4 4 5 4 5 4 4 3 4 4 3 4 4 4 4 4 5 3 4 3 4 3 26 4 4 3 4 9 3 98 5 5 3 6 4 4 4 4 3 4 4 4 4 5 3 5 4 3 4 95 4 4 4 5 4 3 4 3 5 4 3 4 3 3 4 4 4 6 4 3 4 5 4 3 3 3 4 4 3 4 4 4 4 4 4 3 3 5 5 4 5 4 3 4 4 3 3 3 5 4 4 4 6 4 5 5 5 4 3 5 4 4 3 4 15 4 3 3 4 4 3 5 4 4 3 5 3 4 4 3 3 3 4 5 98 5 105 4 4 4 3 4...
result:
wrong answer 28th numbers differ - expected: '9', found: '26'