其他分享
首页 > 其他分享> > CF1325F Ehab's Last Theorem

CF1325F Ehab's Last Theorem

作者:互联网

传送门


思路

dfs 树的一道出色的应用题

令 \(k=\lceil \sqrt n \rceil\)

我们先按照遍历的顺序构建出 dfs 树

对于一条返祖边 \((u, v)\),如果有 \(dep_u-dep_v +1\ge k\),那么 dfs 树上的链 \((v, u)\) 就是一个满足要求的环

假如并没有满足要求的环,说明对于从根出发的链上任意两点 \((x,y)\),如果有 \(dep_x+k-1\ge dep_y\),那么在原图中 \(x,y\) 一定没有连边

而且 dfs 树上不位于同一条链上的两点之间也没有连边

于是我们可以按 \(dep\bmod (k-1)\) 进行分组

根据抽屉原理,一定有一个组点的个数 \(\ge k\)


#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#define LL long long
#define FOR(i, x, y) for(int i = (x); i <= (y); i++)
#define ROF(i, x, y) for(int i = (x); i >= (y); i--)
#define PFOR(i, x) for(int i = he[x]; i; i = r[i].nxt)
inline int reads()
{
    int sign = 1, re = 0; char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') sign = -1; c = getchar();}
    while('0' <= c && c <= '9'){re = re * 10 + (c - '0'); c = getchar();}
    return sign * re;
}
int n, m, k;
struct Node
{
    int to, nxt;
}r[400005]; int he[100005];
std::bitset<200005> rvis;
inline void Edge_add(int u, int v)
{
    static int cnt = 1;
    r[++cnt] = (Node){v, he[u]};
    he[u] = cnt;
}
std::bitset<100005> vis;
int dep[100005], fa[100005];
std::vector<int> q[100005];
void dfs(int now)
{
    vis[now] = 1; q[dep[now] % (k - 1)].emplace_back(now);
    PFOR(i, now)
    {
        if(rvis[i >> 1]) continue;
        int to = r[i].to;
        if(vis[to])
        {
            if(dep[now] - dep[to] + 1 >= k)
            {
                printf("2\n%d\n", dep[now] - dep[to] + 1);
                while(1)
                {
                    printf("%d ", now);
                    if(now == to) exit(0);
                    now = fa[now];
                }
            }
        }
        else
        {
            dep[to] = dep[now] + 1;
            fa[to] = now;
            rvis[i >> 1] = 1;
            dfs(to);
        }
    }
}
signed main()
{
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    n = reads(), m = reads(); k = ceil(sqrt(n));
    FOR(i, 1, m)
    {
        int u = reads(), v = reads();
        Edge_add(u, v), Edge_add(v, u);
    }
    FOR(i, 1, n) if(!vis[i])
        dep[i] = 1,
        dfs(i);
    puts("1");
    FOR(i, 0, m - 1) if(q[i].size() >= k)
    {
        FOR(j, 0, k - 1) printf("%d ", q[i][j]);
        break;
    }
    return 0;
}

标签:CF1325F,include,Last,dep,dfs,int,reads,Ehab,now
来源: https://www.cnblogs.com/zuytong/p/16661983.html