QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#187697 | #6634. Central Subset | qzhwlzy | WA | 9ms | 7956kb | C++14 | 1.2kb | 2023-09-24 20:47:57 | 2023-09-24 20:47:58 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cmath>
#define maxn 200005
using namespace std;
int T,n,m,u,v,dep[maxn],maxpos,s[maxn],top,ans[maxn],num=0; bool flag=0;
struct node{int to,nex;}a[maxn]; int head[maxn],cnt=0;
void add(int from,int to){a[++cnt].to=to; a[cnt].nex=head[from]; head[from]=cnt;}
int f[maxn]; int getfa(int x){return f[x]==x?x:f[x]=getfa(f[x]);}
void dfs1(int p,int fa){
dep[p]=dep[fa]+1; maxpos=(dep[p]>dep[maxpos]?p:maxpos);
for(int i=head[p];i;i=a[i].nex) if(a[i].to!=fa) dfs1(a[i].to,p);
}
void dfs2(int p,int fa){
s[++top]=p; if(p==maxpos){flag=1; return;}
for(int i=head[p];i;i=a[i].nex){if(a[i].to!=fa) dfs2(a[i].to,p); if(flag) return;}
}
int main(){
scanf("%d",&T); while(T--){
scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) f[i]=i,head[i]=0; cnt=num=maxpos=flag=top=0;
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v); int r1=getfa(u),r2=getfa(v); if(r1==r2) continue;
f[r2]=r1; add(u,v);
} dfs1(1,0); dfs2(1,0);
int thres=int(sqrt(n)); if(thres*thres!=n) thres++; while(top>0){
for(int i=1;i<=thres;i++){top--; if(top==0) top++;} ans[++num]=s[top]; if(top==1) break;
} printf("%d\n",num); for(int i=1;i<=num;i++) printf("%d ",ans[i]); printf("\n");
} return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7820kb
input:
2 4 3 1 2 2 3 3 4 6 7 1 2 2 3 3 1 1 4 4 5 5 6 6 4
output:
2 2 1 1 1
result:
ok correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 9ms
memory: 7956kb
input:
10000 15 14 13 12 5 4 9 8 11 12 15 14 10 9 14 13 2 3 2 1 6 5 10 11 3 4 7 6 8 7 6 5 2 1 2 4 4 6 2 3 3 5 10 9 8 3 9 4 5 6 5 10 3 2 5 4 2 7 1 2 4 3 2 1 2 1 2 1 2 1 9 8 9 8 5 4 1 2 6 5 3 4 3 2 7 8 7 6 2 1 1 2 14 13 3 10 5 6 2 9 11 4 2 3 2 1 8 7 13 6 5 4 5 12 6 7 4 3 7 14 16 15 2 3 2 1 6 10 6 9 6 4 9 11 ...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 ...
result:
wrong answer Condition failed: "getMaxBfsDist(n, subset) <= csqrtn" (test case 1)