Codeforces 653E:Bear and Forgotten Tree 2

来源:互联网 发布:中国服装出口数据 编辑:程序博客网 时间:2024/05/09 13:58

E. Bear and Forgotten Tree 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A tree is a connected undirected graph consisting of n vertices and n  -  1 edges. Vertices are numbered 1 through n.

Limak is a little polar bear. He once had a tree with n vertices but he lost it. He still remembers something about the lost tree though.

You are given m pairs of vertices (a1, b1), (a2, b2), ..., (am, bm). Limak remembers that for each i there was no edge between ai andbi. He also remembers that vertex 1 was incident to exactly k edges (its degree was equal to k).

Is it possible that Limak remembers everything correctly? Check whether there exists a tree satisfying the given conditions.

Input

The first line of the input contains three integers nm and k () — the number of vertices in Limak's tree, the number of forbidden pairs of vertices, and the degree of vertex 1, respectively.

The i-th of next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th pair that is forbidden. It's guaranteed that each pair of vertices will appear at most once in the input.

Output

Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes).

Examples
input
5 4 21 22 34 24 1
output
possible
input
6 5 31 21 31 41 51 6
output
impossible
Note

In the first sample, there are n = 5 vertices. The degree of vertex 1 should be k = 2. All conditions are satisfied for a tree with edges1 - 55 - 21 - 3 and 3 - 4.

In the second sample, Limak remembers that none of the following edges existed: 1 - 21 - 31 - 41 - 5 and 1 - 6. Hence, vertex 1couldn't be connected to any other vertex and it implies that there is no suitable tree.


题意是给出了n个节点,m个不相连的边,节点1的度确定为k。问给出的关系能否形成一棵树。

做法很亮。。。但后来想一想就是判断联通,联通的点就从set中去掉,然后去看不得不与1相连的点的个数,是否大于k。大于k就不行,否则ok。

代码:

#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x33ffffffconst ll mod = 1000000007;const int maxn = 505;const double PI = acos(-1.0);int n, m, k;set<int>remain;set< pair<int, int> > forbidden;void dfs(int x){vector<int>m;for (auto i : remain){int mx = max(x, i);int mi = min(x, i);if (forbidden.find(make_pair(mi, mx)) == forbidden.end()){m.push_back(i);}}for (auto i : m){remain.erase(i);}for (auto i : m){dfs(i);}}void solve(){int i, j, u, v, com;scanf("%d%d%d", &n, &m, &k);for (i = 1, j = n - 1; i <= m; i++){scanf("%d%d", &u, &v);if (u > v)swap(u, v);if (u == 1)j--;forbidden.insert(make_pair(u, v));}if (j < k){puts("impossible");return;}for (i = 2; i <= n; i++){remain.insert(i);}com = 0;for (i = 2; i <= n; i++){if (forbidden.find(make_pair(1, i)) == forbidden.end() && remain.find(i) != remain.end()){com++;dfs(i);remain.erase(i);}}if (com > k || !remain.empty()){puts("impossible");return;}puts("possible");}int main(){#ifndef ONLINE_JUDGE  freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endifsolve();//system("pause");return 0;}



0 0
原创粉丝点击