QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#702128#6634. Central Subsetgates_orzWA 22ms7996kbC++203.0kb2024-11-02 15:21:362024-11-02 15:21:38

Judging History

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

  • [2024-11-02 15:21:38]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:7996kb
  • [2024-11-02 15:21:36]
  • 提交

answer

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

using LL=long long;
const int N=2e5+10;
const int M=N*2;
int n,m;
int h[N],e[M],ne[M],idx;
int dep[N];
int f[N][20];
void add(int a,int b) {
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
int fa[N];
void init() {
    for(int i=1;i<=n;i++)fa[i]=i;
}
int find(int x) {
    if(x==fa[x])return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
void unionn(int i,int j) {
    int x=find(i),y=find(j);
    fa[y]=x;
}

void solve() {
    cin>>n>>m;
    int k=ceil(pow(n,0.5));
    idx=0;
    init();
    for(int i=1;i<=n;i++)h[i]=-1;
    for(int i=1;i<=m;i++) {
        int a,b;
        cin>>a>>b;
        if(find(a)!=find(b)) {
            unionn(a,b);
            add(a,b),add(b,a);
        }
    }
    //cerr<<"???"<<"\n";
    vector<int>leaf;
    auto dfs=[&](auto &self,int u,int fat)->void {
        dep[u]=dep[fat]+1;
        f[u][0]=fat;
        for(int i=1;i<=18;i++) {
            f[u][i]=f[f[u][i-1]][i-1];
        }
        int sign=1;
        for(int i=h[u];i!=-1;i=ne[i]) {
            int v=e[i];
            if(v==fat)continue;
            sign=0;
            self(self,v,u);
        }
        if(sign)leaf.push_back(u);
    };
    dfs(dfs,1,0);
    int root=leaf.back();
    leaf.clear();
    dfs(dfs,root,0);
    vector<int>vis(n+1,0);
    auto cmp=[&](int a,int b) {
        return dep[a]<dep[b];
    };
    priority_queue<int,vector<int>,decltype(cmp)>heap(cmp);
    for(auto tmp:leaf) {
        heap.push(tmp);
    }
    vector<int>res;
    //cerr<<"? "<<"\n";
    auto work=[&](auto &self,int x) {
        //cerr<<"x="<<x<<" \n";
        if(vis[x])return;
        vis[x]=1;
        for(int i=h[x];i!=-1;i=ne[i]) {
            int v=e[i];
            if(v==f[x][0])continue;
            self(self,v);
        }
    };
    //cerr<<"k="<<k<<"\n";
    while(!heap.empty()) {
        auto tmp=heap.top();
        //cerr<<"tmp="<<tmp<<"\n";
        heap.pop();
        if(vis[tmp])continue;
        int k_fa=tmp;
        int t=k;
        for(int i=18;i>=0;i--) {
            if(t>=1<<i) {
                t-=(1<<i);
                k_fa=f[k_fa][i];
            }
        }
        //if(dep[tmp]-dep[k_fa]!=k)continue;
        if(k_fa==0) {
            res.push_back(1);
            continue;
        }
        //cerr<<"tmp="<<tmp<<" fa="<<k_fa<<"\n";
        res.push_back(k_fa);
        work(work,k_fa);

        if(f[k_fa][0]>1)heap.push(f[k_fa][0]);
    }
    //cerr<<"?????"<<"\n";

    sort(res.begin(),res.end());
    res.erase(unique(res.begin(),res.end()),res.end());
    if(res.size()==0)res.push_back(1);
    if(res.size()>k) {
        cout<<"-1"<<"\n";
        return;
    }
    cout<<res.size()<<"\n";
    for(auto tmp:res) {
        cout<<tmp<<" ";
    }
    cout<<"\n";

}
/*
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
 */
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    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: 7996kb

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
1 3 
1
1 

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 22ms
memory: 7992kb

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:

3
5 10 15 
2
1 4 
1
8 
1
1 
1
1 
3
1 4 8 
1
1 
2
1 4 
2
6 13 
1
1 
4
1 6 12 18 
2
1 4 
2
1 2 
2
1 10 
1
1 
3
5 10 15 
2
1 4 
1
1 
2
1 2 
1
1 
2
1 3 
2
1 4 
2
1 4 
2
5 12 
1
1 
3
5 10 15 
2
1 3 
2
5 10 
1
3 
1
1 
4
1 6 12 18 
2
1 2 
2
1 4 
3
1 6 7 
1
1 
2
1 4 
2
1 4 
2
1 3 
3
1 6 7 
1
1 
3
1 5 10 
2
...

result:

wrong answer Condition failed: "getMaxBfsDist(n, subset) <= csqrtn" (test case 3442)