CF 1325F - Ehab's Last Theorem

First of all:

Undirected graph is a tree without lateral edge dfs

It is clear, because if u-> v cross edge, then v would have come from this side of the ... u find contradictions

Set \ (k = ceil (\ sqrt {n}) \)

Next run dfs tree, if there is such a ring back sides greater than or equal k, outputs the ring.

Otherwise, indicating a problem: The number of back side of all the points are less than k (otherwise sure to find the ring, the pigeonhole principle ~)

Thus, the stop can be selected from the bottom up to the point independent set point, while the leading edge of it can not be taken to be marked as an independent set.

Results must be at least two independent sets of k points.

why? As said earlier, the number of back side at all points less than k, so to take the point, every time point to at most k-1 marker is a dependent set point.

Tags, flag [i] = 1 indicates \ (I \) have been identified as not independent set point.

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

const int N = 1e5+5;
bool flag[N];
int fa[N], dep[N];

int n,m,k;
vector<int> G[N];

void dfs(int x,int f,int d) {
    dep[x] = d;
    fa[x] = f;
    for(auto y:G[x]) {
        if(dep[y]==0)
            dfs(y,x,d+1);
        if(dep[x]-dep[y]+1>=k) {
            cout<<2<<endl;
            cout<<dep[x]-dep[y]+1<<endl;
            cout<<y<<" ";
            int z = x;
            while(z!=y) {
                cout<<z<<" ";
                z = fa[z];
            }
            exit(0);
        }
    }
    if(!flag[x])
        for(auto y:G[x]) {
            flag[y] = 1;
        } 
}

int main() {
    ios::sync_with_stdio(0);
    cin>>n>>m;
    while(k*k<n) k++;

    for(int i=0,a,b;i<m;i++) {
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(1,0,1);
    cout<<1<<endl;
    for(int i=1; k; i++) {
        if(flag[i]) continue;
        cout<<i<<" ";
        k--;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/scnucjh/p/12624151.html