Given a connected undirected graph with vertices and an integer , you have to either:
- either find an independent set that has exactly vertices.
- or find a simple cycle of length at most .
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 , , and — the number of vertices and edges in the graph, and the parameter from the statement.
Each of the next lines contains two integers and that mean there’s an edge between vertices and . 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 , followed by a line containing distinct integers not exceeding , the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print , followed by a line containing one integer, , representing the length of the found cycle, followed by a line containing distinct integers not exceeding , 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 is also OK, but printing the cycle isn’t, because its length must be at most .
In the second sample:
Notice that printing the independent set or printing the cycle is also OK.
In the third sample:
In the fourth sample:
水題一道。
分情況討論,當所給圖爲一棵樹的時候對其黑白染色,使得相同的顏色不相鄰,從染色數較多的顏色對應的點中選個輸出即可。
否則先將圖連成一棵樹,然後選出端點深度最小的邊與樹構成一個簡單環,如果環上的點的數量小於等於,則將環輸出,否則將環上的點間隔輸出個。
#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;
}