QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#785493#6421. Degree of Spanning TreelfyszyRE 2ms9040kbC++143.4kb2024-11-26 18:04:562024-11-26 18:04:57

Judging History

你现在查看的是最新测评结果

  • [2024-11-26 18:04:57]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:9040kb
  • [2024-11-26 18:04:56]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef double db;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
template <typename Type>
using vec=vector<Type>;
template <typename Type>
using grheap=priority_queue<Type>;
template <typename Type>
using lrheap=priority_queue<Type,vector<Type>,greater<Type> >;
#define fir first
#define sec second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)

const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7/*998244353*/;

const int N=1e5+5,M=2e5+5;

int n,m;
int u,v;
int rt;
int dg[N], d[N];
struct edge{
    int a,b;
}es[M];
bool use[M];
int dte[N];

struct DSU{
    int fa[N];
    void init(int n){
        rep(i,1,n)fa[i]=i;
    }
    int find(int x){
        if(fa[x]!=x)fa[x]=find(fa[x]);
        return fa[x];
    }
    void merge(int a,int b){
        a=find(a);b=find(b);
        fa[a]=b;
    }
    bool same(int a,int b){
        return find(a)==find(b);
    }
}dsu;

void outans(){
    cout<<"Yes\n";
    rep(k,1,m){
        if(use[k])cout<<es[k].a<<" "<<es[k].b<<"\n";
    }
}

vec<int> g[N];
bool vis[N];
void dfs(int now,int fa){
    vis[now]=true;
    dsu.merge(now,fa);
    for(auto nxt:g[now]){
        if(vis[nxt])continue;
        dfs(nxt,fa);
    }
}

void solve(){
    cin>>n>>m;
    rep(i,1,n)dg[i]=0;
    rep(i,1,m){
        cin>>u>>v;
        use[i]=0;
        es[i].a=u;es[i].b=v;
    }
    dsu.init(n);
    rep(i,1,n)g[i].clear();
    rep(i,1,m){
        if(!dsu.same(es[i].a,es[i].b)){
            dsu.merge(es[i].a,es[i].b);
            use[i]=1;
            dg[es[i].a]++;
            dg[es[i].b]++;
        }
    }
    rt=0;
    rep(i,1,n){
        if(dg[i]>n/2){
            rt=i;
            break;
        }
    }
    if(!rt){
        outans();
        return;
    }
    rep(i,1,m){
        if(es[i].a==rt)dte[es[i].b]=i;
        if(es[i].b==rt)dte[es[i].a]=i;
    }
    rep(i,1,m){
        if(use[i]){
            g[es[i].a].pub(es[i].b);
            g[es[i].b].pub(es[i].a);
        }
    }
    dsu.init(n);
    rep(i,1,n)vis[i]=false;
    vis[rt]=true;
    for(auto son:g[rt]){
        dfs(son,son);
    }
    rep(i,1,m){
        if(use[i])continue;
        // ||es[i].a==rt||es[i].b==rt
        if(dsu.same(es[i].a,es[i].b))continue;
        int fa = dsu.find(es[i].a), fb = dsu.find(es[i].b);
        if(dg[fa] > dg[fb]) swap(fa, fb);
        dg[es[i].a]++;dg[es[i].b]++;
        dg[rt]--,dg[fb]--;
        if(dg[es[i].a]>n/2||dg[es[i].b]>n/2){
            dg[es[i].a]--;dg[es[i].b]--;
            dg[rt]++;dg[fb]++;
            continue;
        }
        use[i]=true;
        use[dte[fb]]=false;
        dsu.merge(fb,fa);
        if(dg[rt]<=n/2)break;
    }
    for(int i = 1; i <= m; i ++) if(use[i]) {d[es[i].a]++;d[es[i].b]++;}
    for(int i = 1; i <= n; i ++) assert(dg[i] == d[i]);
    if(dg[rt]<=n/2)outans();
    else cout<<"No\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;cin>>t;
    while(t--)solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9040kb

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
Runtime Error

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:


result: