QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#612324 | #6421. Degree of Spanning Tree | Soestx | WA | 128ms | 11584kb | C++23 | 2.1kb | 2024-10-05 10:30:39 | 2024-10-05 10:30:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
struct node{
int u,v,id;
node(int u=0,int v=0,int id=0):u(u),v(v),id(id){}
}e[maxn];
vector<node>G[maxn];
bool vis[maxn];
int f[maxn],in[maxn],fa[maxn],dep[maxn];
void add(int u,int v,int id){
G[u].push_back(node(u,v,id));
G[v].push_back(node(v,u,id));
}
int findx(int x){
return f[x]==x?x:f[x] = findx(f[x]);
}
void unite(int x,int y){
x = findx(x),y = findx(y);
if(x==y) return ;
f[x] = y;
}
bool same(int x,int y){
return findx(x)==findx(y);
}
void dfs(int u,int pre,int t){
f[u] = t,dep[u] = dep[pre]+1;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
if(v==pre) continue;
dfs(v,u,t);
}
}
void print(int m){
printf("Yes\n");
for(int i=1;i<=m;i++){
if(vis[i]) printf("%d %d\n",e[i].u,e[i].v);
}
}
int main(){
int T;
scanf("%d",&T);
while (T--){
int n,m,rt = 0;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++) f[i] = i,G[i].clear(),in[i] = 0;
for(int i=0;i<=m;i++) vis[i] = false;
for(int i=1;i<=m;i++) {
int u,v;
scanf("%d%d",&u,&v);
e[i] = node(u,v,i);
if(!same(u,v)) unite(u,v),vis[i] = true,in[u]++,in[v]++,add(u,v,i);
if(in[u]>in[rt]) rt = u;
if(in[v]>in[rt]) rt = v;
}
if(n==3){
printf("No\n");
continue;
}
dep[rt] = 0,f[rt] = rt;
for(int i=0;i<G[rt].size();i++){
int v = G[rt][i].v;
fa[v] = G[rt][i].id;
dfs(v,rt,v);
}
for(int i=1;i<=m;i++){
if(in[rt]<=n/2) break;
int u = e[i].u,v = e[i].v;
int fu = findx(u),fv = findx(v);
if(fu==fv||u==rt||v==rt) continue;
++in[u],++in[v],--in[rt],--in[fv];
vis[e[i].id] = true,vis[fa[fv]] = false;
f[fv] = fu;//!!!!
}
if(in[rt]>n/2) printf("No\n");
else print(m);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9224kb
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: -100
Wrong Answer
time: 128ms
memory: 11584kb
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:
wrong answer case 153, paticipant's deg[4] = 3 is too large