Codeforces 653E:Bear and Forgotten Tree 2
来源:互联网 发布:中国服装出口数据 编辑:程序博客网 时间:2024/05/09 13:58
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.
The first line of the input contains three integers n, m 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.
Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes).
5 4 21 22 34 24 1
possible
6 5 31 21 31 41 51 6
impossible
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 - 5, 5 - 2, 1 - 3 and 3 - 4.
In the second sample, Limak remembers that none of the following edges existed: 1 - 2, 1 - 3, 1 - 4, 1 - 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;}
- Codeforces 653E:Bear and Forgotten Tree 2
- codeforces 653E. Bear and Forgotten Tree 2 连通问题
- CodeForces 653E Bear and Forgotten Tree 2
- CF 653E(Bear and Forgotten Tree 2-链表优化搜索)
- [BFS] CF653E. Bear and Forgotten Tree 2
- Codeforces 658C Bear and Forgotten Tree 3【构造】
- CodeForces 639B Bear and Forgotten Tree 3(水题)
- CodeForces 658C Bear and Forgotten Tree 3(构造)
- Codeforces 639B Bear and Forgotten Tree 3 【构造】
- CodeForces-657A-Bear and Forgotten Tree 3
- CodeForces 657A Bear and Forgotten Tree 3
- CodeForces 639 B.Bear and Forgotten Tree 3(构造)
- Codeforces 658C Bear and Forgotten Tree 3【思维】
- codeforces 658C Bear and Forgotten Tree 3构造-多么痛的领悟
- Codeforces 658 C. Bear and Forgotten Tree 3(树的构造)
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- [杂题 图论 构造] Codeforces #639B. Bear and Forgotten Tree 3
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树★ ★
- linux文本处理三剑客
- Spring中用注解实现bean的定义以及作用域
- BestCoder #76
- React-Native系列Android——自定义View组件开发
- Objective c ,iOS KVC是如何实现的,权威答案
- Codeforces 653E:Bear and Forgotten Tree 2
- Oracle数据库学习(三)--基础查询及关联查询
- Eclipse安装颜色主题插件eclipse color theme
- python学习(二):函数
- Mac 10.12.1 安装cocoapods(淘宝镜像用不了的)
- 广域网详解
- RelativeLayout相对布局
- 简单的警告对话框
- hdu1237简单计算器(栈的简单运用)