80522024-01-12 11:31:07UVinceHálózati biztonság (50)cpp17Accepted 50/50167ms31284 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

struct edge{
    int a;
    int b;
    bool active=true;
};


int n,m,k;
vector<edge> edges;
vector<vector<int>> adj;
vector<int> kifok;
vector<bool> done;
vector<bool> vis;
vector<vector<int>> ans;

int dfs(int node){
    if (vis[node]) return 0;
    vis[node]=true;
    int ret=1;
    ans.back().push_back(node);
    for (int i : adj[node]){
        if (edges[i].active){
            if (edges[i].a!=node) swap(edges[i].a, edges[i].b);
            ret+=dfs(edges[i].b);
        }
    }
    return ret;
}


int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>m>>k;
    adj.resize(n+1);
    edges.resize(m);
    kifok.resize(n+1);
    done.resize(n+1, false);
    vis.resize(n+1,false);
    for (int i = 0; i < m; i++)
    {
        int a,b;
        cin>>a>>b;
        edges[i]={a,b};
        adj[a].push_back(i);
        adj[b].push_back(i);
        kifok[a]++;
        kifok[b]++;
    }
    priority_queue<array<int,2>, vector<array<int,2>>, greater<array<int,2>>> sor;
    for (int i=1;i<=n;i++) sor.push({kifok[i],i});
    while (sor.top()[0]<k){
        auto [ki, node] = sor.top();
        sor.pop();
        if (done[node]) continue;
        done[node]=true;
        for (int i : adj[node]){
            edge& el=edges[i];
            if(!el.active) continue;
            kifok[el.a]--;
            kifok[el.b]--;
            el.active=false;
            if (!done[el.a]) sor.push({kifok[el.a], el.a});
            if (!done[el.b]) sor.push({kifok[el.b], el.b});
        }
    }
    int ma=INT_MIN,maxi=-1;
    for (int i=1;i<=n;i++) {
        if (!done[i] && !vis[i]) {
            ans.push_back({});
            int si=dfs(i);
            ma=max(ma,si);
            maxi=(ma=si) ? ans.size()-1 : maxi;
        }
    }
    if (maxi==-1){
        cout<<"0";
        return 0;
    }
    cout<<ans[maxi].size()<<"\n";
    sort(ans[maxi].begin(), ans[maxi].end());
    for (int i : ans[maxi]) cout<<i<<" ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1864 KiB
2Accepted0/086ms14368 KiB
3Accepted2/23ms3404 KiB
4Accepted2/23ms3632 KiB
5Accepted2/23ms3856 KiB
6Accepted2/23ms4060 KiB
7Accepted2/23ms4156 KiB
8Accepted2/23ms4520 KiB
9Accepted2/23ms4748 KiB
10Accepted2/26ms5408 KiB
11Accepted2/23ms4984 KiB
12Accepted2/24ms5644 KiB
13Accepted3/34ms5620 KiB
14Accepted3/38ms6824 KiB
15Accepted3/310ms7828 KiB
16Accepted3/379ms16152 KiB
17Accepted3/38ms7768 KiB
18Accepted3/317ms11780 KiB
19Accepted3/3101ms23584 KiB
20Accepted3/3167ms31284 KiB
21Accepted3/3105ms27492 KiB
22Accepted3/33ms11484 KiB