QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#208594 | #6421. Degree of Spanning Tree | qzhwlzy | RE | 773ms | 5916kb | C++14 | 1.7kb | 2023-10-09 19:11:02 | 2023-10-09 19:11:02 |
Judging History
answer
#include<iostream>
#include<cstdio>
#define maxn 100005
#define maxm 200005
using namespace std;
int T,n,m,x[maxm],y[maxm],f[maxn],fa[maxn],deg[maxn],pos; bool vis[maxm],rt[maxn];
struct{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 getfa(int p){return f[p]==p?p:getfa(f[p]);}
void dfs(int p,int fat){
for(int i=head[p];i;i=a[i].nex) if(a[i].to!=fat)
{if(p!=pos){int r1=getfa(p),r2=getfa(a[i].to); f[r2]=r1;}else rt[a[i].to]=1; dfs(a[i].to,p);}
}
int main(){
scanf("%d",&T); while(T--){
scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) head[i]=deg[i]=0,f[i]=i; cnt=0;
for(int i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]),vis[i]=0;
for(int i=1;i<=m;i++){
int r1=getfa(x[i]),r2=getfa(y[i]); if(r1==r2) continue;
f[r2]=r1; add(x[i],y[i]); add(y[i],x[i]); vis[i]=1; deg[x[i]]++; deg[y[i]]++;
}
pos=-1; for(int i=1;i<=n;i++){
if(deg[i]>n/2){pos=i; break;}
if(i==n){printf("Yes\n"); for(int i=1;i<=m;i++) if(vis[i]) printf("%d %d\n",x[i],y[i]);}
} if(pos==-1) continue; for(int i=1;i<=n;i++) f[i]=i; dfs(pos,-1);
for(int i=1;i<=m;i++) if(!vis[i]){
int r1=getfa(x[i]),r2=getfa(y[i]); if(r1!=r2&&r1!=pos&&r2!=pos){
deg[pos]--; deg[r1]--; deg[x[i]]++; deg[y[i]]++;
if(deg[x[i]]>n/2||deg[y[i]]>n/2){
deg[r1]++; deg[r2]--;
if(deg[x[i]]>n/2||deg[y[i]]>n/2){deg[pos]++; deg[r2]++; deg[x[i]]--; deg[y[i]]--; continue;}
f[r2]=r1; vis[i]=1; rt[r2]=0; continue;
}
f[r1]=r2; vis[i]=1; rt[r1]=0;
}
} if(deg[pos]>n/2){printf("No\n"); continue;} printf("Yes\n"); int cnt=0;
for(int i=1;i<=m&&cnt<n;i++) if(vis[i]&&!((x[i]==pos&&!rt[y[i]])||(y[i]==pos&&!rt[x[i]])))
printf("%d %d\n",x[i],y[i]),cnt++;
} return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5916kb
input:
2 6 9 1 2 1 3 1 4 2 3 2 4 3 4 4 5 4 6 4 6 3 4 1 3 2 3 3 3 1 2
output:
Yes 1 2 1 3 1 4 4 5 4 6 No
result:
ok 2 cases
Test #2:
score: 0
Accepted
time: 773ms
memory: 5804kb
input:
11140 10 15 9 6 5 7 6 5 2 3 7 5 7 5 3 10 9 7 5 5 9 1 7 5 2 8 7 5 4 3 6 2 9 19 3 7 3 9 2 8 2 8 3 6 5 1 1 8 8 9 8 3 4 8 5 5 3 1 4 3 1 3 8 6 1 3 7 4 4 3 8 8 12 20 10 2 5 5 2 4 3 3 3 3 5 11 9 2 5 5 7 12 11 3 3 3 3 5 5 3 3 1 4 6 7 11 6 8 4 5 6 12 6 5 8 18 4 2 4 3 2 4 2 4 4 3 4 8 2 2 6 7 2 4 6 2 1 4 8 7 4...
output:
Yes 9 6 5 7 6 5 2 3 3 10 9 1 2 8 4 3 6 2 Yes 3 7 3 9 2 8 3 6 5 1 1 8 8 9 4 8 Yes 10 2 2 4 5 11 9 2 7 12 11 3 3 1 4 6 7 11 6 8 4 5 Yes 4 2 4 3 4 8 6 7 6 2 1 4 7 5 Yes 6 5 5 7 5 9 4 3 2 9 2 3 8 7 5 1 Yes 10 2 2 6 3 2 1 9 8 10 4 6 6 1 2 5 1 7 Yes 5 7 5 4 7 1 2 6 6 7 1 3 Yes 12 3 1 13 7 8 8 2 10 6 1 6 1...
result:
ok 11140 cases
Test #3:
score: -100
Runtime Error
input:
5 100000 197799 37555 22723 91160 32701 6451 4416 43186 26432 9750 82955 28292 33907 91534 78442 17771 67061 40351 28116 21494 23114 34672 66640 72636 95093 13033 6538 53698 87837 79541 71230 53985 63500 84753 5378 67971 56748 90951 20169 4465 97293 18331 53564 41043 95738 48579 96439 90865 7526 391...