QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#739567 | #852. Jellyfish | jiangzhihui# | AC ✓ | 123ms | 16332kb | C++14 | 2.4kb | 2024-11-12 22:13:52 | 2024-11-12 22:13:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct graph{
int head[N],nxt[N<<1],to[N<<1],cnt;
graph():cnt(1){}
void add(int u,int v){
nxt[++cnt]=head[u];
to[cnt]=v;
head[u]=cnt;
}
}gr;
int n,fa[N];
bool vis[N<<1];
bool tag[N];
vector<int> cir;
bool incir[N];
void dfs(int u){
tag[u]=true;
for(int i=gr.head[u];i;i=gr.nxt[i]){
if(vis[i])continue;
vis[i]=vis[i^1]=true;
int v=gr.to[i];
if(tag[v]){
int t=u;
while(t!=v){
incir[t]=true;
cir.push_back(t);
t=fa[t];
}
incir[v]=true;
cir.push_back(v);
break;
}else{
fa[v]=u;
dfs(v);
}
}
}
int sz[N];
int tot;
void dfs_tree(int u,int fa){
sz[u]=1;
for(int i=gr.head[u];i;i=gr.nxt[i]){
int v=gr.to[i];
if(incir[v])continue;
if(v==fa)continue;
dfs_tree(v,u);
sz[u]+=sz[v];
}
if(sz[u]==1)tot++;
}
void clear(){
for(int i=2;i<=gr.cnt;i++){
vis[i]=false;
}
gr.cnt=1;
for(int i=1;i<=n;i++){
gr.head[i]=0;
incir[i]=false;
tag[i]=false;
fa[i]=0;
sz[i]=0;
}
tot=0;
cir.clear();
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
int u,v;
cin>>u>>v;
gr.add(u,v);
gr.add(v,u);
}
dfs(1);
int ans=0;
int d=0;
for(auto x:cir){
tot=0;
dfs_tree(x,0);
if(sz[x]>1){
ans+=tot;
d++;
}
}
if(d==0){
cout<<min(3,(int)cir.size())<<'\n';
}else if(d==1){
cout<<ans+min((int)cir.size()-1,2)<<'\n';
}else{
int las=-1;
int mx=0;
int fir=-1;
for(int i=0;i<cir.size();i++){
int x=cir[i];
if(sz[x]>1){
mx=max(mx,min(i-las-1,2));
if(fir==-1){
fir=i;
}
las=i;
}
}
mx=max(mx,min(2,(int)cir.size()-(las-fir+1)));
cout<<ans+mx<<'\n';
}
clear();
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5760kb
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: 0
Accepted
time: 93ms
memory: 5768kb
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 9 4 4 3 4 8 3 98 5 4 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 4 4 3 4 4 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 10 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:
ok 85665 numbers
Test #3:
score: 0
Accepted
time: 123ms
memory: 16332kb
input:
30 2229 1066 248 248 881 881 2080 2080 1615 1615 1647 1647 1774 1774 196 196 434 434 390 390 129 129 563 563 63 63 1457 1457 1015 1015 2200 2200 1187 1187 1763 1763 1121 1121 2122 2122 1783 1783 1756 1756 2031 2031 2153 2153 605 605 1778 1778 1287 1287 2062 2062 817 817 194 194 474 474 414 414 1736 ...
output:
1092 881 722 1412 37556 638 438 509 273 29198 740 27535 46011 865 444 30031 49564 794 489 469 624 956 1180 17384 50000 715 1291 49920 1465 3
result:
ok 30 numbers