Ehabs Last Corollary

Given a connected undirected graph with nn vertices and an integer kk, you have to either:

  • either find an independent set that has exactly k2⌈\frac{k}{2}⌉ vertices.
  • or find a simple cycle of length at most kk.

An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn’t contain any vertex twice.

I have a proof that for any input you can always solve at least one of these problems, but it’s left as an exercise for the reader.

Input
The first line contains three integers nn, mm, and kk (3kn105,n1m2105)(3≤k≤n≤10^5, n−1≤m≤2⋅10^5) — the number of vertices and edges in the graph, and the parameter kk from the statement.

Each of the next mm lines contains two integers uu and vv (1u,vn)(1≤u,v≤n) that mean there’s an edge between vertices uu and vv. It’s guaranteed that the graph is connected and doesn’t contain any self-loops or multiple edges.

Output
If you choose to solve the first problem, then on the first line print 11, followed by a line containing k2⌈k_2⌉ distinct integers not exceeding nn, the vertices in the desired independent set.

If you, however, choose to solve the second problem, then on the first line print 22, followed by a line containing one integer, cc, representing the length of the found cycle, followed by a line containing cc distinct integers not exceeding nn, the vertices in the desired cycle, in the order they appear in the cycle.

Examples

input
4 4 3
1 2
2 3
3 4
4 1
output
1
1 3 
input
4 5 3
1 2
2 3
3 4
4 1
2 4
output
2
3
2 3 4 
input
4 6 3
1 2
2 3
3 4
4 1
1 3
2 4
output
2
3
1 2 3 
input
5 4 5
1 2
1 3
2 4
2 5
output
1
1 4 5 

Note
In the first sample:

在這裏插入圖片描述

Notice that printing the independent set 2,4{2,4} is also OK, but printing the cycle 12341−2−3−4 isn’t, because its length must be at most 33.

In the second sample:

在這裏插入圖片描述

Notice that printing the independent set 1,3{1,3} or printing the cycle 2142−1−4 is also OK.

In the third sample:

在這裏插入圖片描述
In the fourth sample:

在這裏插入圖片描述
水題一道。
分情況討論,當所給圖爲一棵樹的時候對其黑白染色,使得相同的顏色不相鄰,從染色數較多的顏色對應的點中選k2⌈\frac{k}{2}⌉個輸出即可。
否則先將圖連成一棵樹,然後選出端點深度最小的邊與樹構成一個簡單環,如果環上的點的數量小於等於kk,則將環輸出,否則將環上的點間隔輸出k2⌈\frac{k}{2}⌉個。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e5 + 10, M = 2e5 + 10;
int head[N], ver[M << 1], Next[M << 1], tot;

struct Union_Find {
    int fa[N];

    void init(int n) {
        repi(i, 0, n) fa[i] = i;
    }

    int find(int x) {
        return fa[x] == x ? x : fa[x] = find(fa[x]);
    }

    void unit(int x, int y) {
        x = find(x), y = find(y);
        if (x != y) fa[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
} uf;

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

vector<pii > e;
int n, m, k;
int col[N];

struct LCA {
    int t, f[N][20], d[N];

    void dfs(int x) {
        for (int i = head[x]; i; i = Next[i]) {
            int y = ver[i];
            if (d[y])continue;
            d[y] = d[x] + 1;
            f[y][0] = x;
            for (int j = 1; j <= t; j++)
                f[y][j] = f[f[y][j - 1]][j - 1];
            dfs(y);
        }
    }

    inline int lca(int x, int y) {
        if (d[x] > d[y])swap(x, y);
        for (int i = t; i >= 0; i--)
            if (d[f[y][i]] >= d[x])
                y = f[y][i];
        if (x == y)
            return x;
        for (int i = t; i >= 0; i--)
            if (f[x][i] != f[y][i])
                x = f[x][i], y = f[y][i];
        return f[x][0];
    }

    int dis(int x, int y) {
        return d[x] + d[y] - 2 * d[lca(x, y)];
    }
} L;

void colour(int x, int c) {
    col[x] = c;
    reps(x) {
        int y = ver[i];
        if (col[y])continue;
        colour(y, 3 - c);
    }
}

int main() {
    n = qr(), m = qr(), k = qr();
    if (m == n - 1) {
        k = ceil((k * 1.0) / 2.0);
        repi(i, 1, n - 1) {
            int x = qr(), y = qr();
            add(x, y), add(y, x);
        }
        colour(1, 1);
        int cnt[3] = {0};
        repi(i, 1, n)cnt[col[i]]++;
        int c;
        c = cnt[1] > cnt[2] ? 1 : 2;
        puts("1");
        int ct = 0;
        repi(i, 1, n)
            if (col[i] == c) {
                printf("%d ", i), ct++;
                if (ct == k)break;
            }
        return 0;
    }
    uf.init(n);
    while (m--) {
        int x = qr(), y = qr();
        if (uf.same(x, y))e.pb({x, y});
        else add(x, y), add(y, x), uf.unit(x, y);
    }
    L.d[1] = 1, L.t = log2(n) + 1, L.dfs(1);
    pii res = {0, 0};
    for (auto it:e)
        if (!res.fi || L.d[it.fi] < L.d[res.fi] || L.d[it.fi] == L.d[res.fi] && L.d[it.se] < L.d[res.se])
            res = it;
    int x = res.fi, y = res.se;
    int lca = L.lca(x, y);
    vi seq;
    while (x != lca)seq.pb(x), x = L.f[x][0];
    seq.pb(lca);
    vi tmp;
    while (y != lca)tmp.pb(y), y = L.f[y][0];
    reverse(tmp.begin(), tmp.end());
    for (auto it:tmp)seq.pb(it);
    if (seq.size() <= k) {
        puts("2");
        pi(seq.size());
        for (auto it:seq)
            printf("%d ", it);
    } else {
        puts("1");
        k = ceil((k * 1.0) / 2.0);
        int ct = 0;
        for (int i = 0; i <= seq.size() - 1; i += 2) {
            printf("%d ", seq[i]), ct++;
            if (ct == k)break;
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章