Flipping Cards 图论 强连通分量 2015 Rocky Mountain Regional Programming Contest
2017-07-06 19:53
746 查看
Problem B Flipping Cards
Accept: 0 Submit: 0
Mike and his young daughter Jesse are playing a new card game meant
for kids. The rules are quite simple, each player is dealt a hand of cards.
Each card has one picture on each side. They take turns playing cards
and the first one to run out of cards is the winner.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201605/7914d919372d1749605ca8cd6276359f)
A player’s turn consists of picking a subset of cards from their hand and
laying them on the table. The only rule is that the cards must be placed
on the table such that no two cards are showing the same picture.
Mike thought this was a very appropriate game to play with his kid because of the simple rules. Mike also
liked this game because finding the best strategy is an algorithmically interesting challenge!
Help Mike determine if he can play his entire hand on his first round.
The first line of the input contains a single positive integer T (T ≤ 10) indicating the number of test cases. Each test case begins with a single integer n denoting the number of cards in Mike’s hand. Here 1 ≤ n ≤ 50 000. Following this are n lines, each describing
a card in Mike’s hand. The pictures on the cards are represented by integers. The ith card is given by two integers pi , qi where 1 ≤ pi , qi ≤ 2n.
For each test case you should output a single line with the word possible if it ispossible for Mike to play his entire hand in one turn, orimpossible if Mike cannot play his entire hand in one turn.
3
3
1 2
1 3
2 3
3
1 2
1 2
1 2
1
1 1
possible
impossible
possible
图论题,将每个牌两面的数连边,求强连通分量,同一个分量中点数不能少于边数。因为,每条边代表一张牌,每个点代表一个数字,如果分量中边数多于点数则不能使每张牌都有对应的牌。
Accept: 0 Submit: 0
Time Limit: 5000 mSec
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Problem
Description
Mike and his young daughter Jesse are playing a new card game meantfor kids. The rules are quite simple, each player is dealt a hand of cards.
Each card has one picture on each side. They take turns playing cards
and the first one to run out of cards is the winner.
A player’s turn consists of picking a subset of cards from their hand and
laying them on the table. The only rule is that the cards must be placed
on the table such that no two cards are showing the same picture.
Mike thought this was a very appropriate game to play with his kid because of the simple rules. Mike also
liked this game because finding the best strategy is an algorithmically interesting challenge!
Help Mike determine if he can play his entire hand on his first round.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Input
The first line of the input contains a single positive integer T (T ≤ 10) indicating the number of test cases. Each test case begins with a single integer n denoting the number of cards in Mike’s hand. Here 1 ≤ n ≤ 50 000. Following this are n lines, each describinga card in Mike’s hand. The pictures on the cards are represented by integers. The ith card is given by two integers pi , qi where 1 ≤ pi , qi ≤ 2n.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Output
For each test case you should output a single line with the word possible if it ispossible for Mike to play his entire hand in one turn, orimpossible if Mike cannot play his entire hand in one turn.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Sample
Input
33
1 2
1 3
2 3
3
1 2
1 2
1 2
1
1 1
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Sample
Output
possibleimpossible
possible
图论题,将每个牌两面的数连边,求强连通分量,同一个分量中点数不能少于边数。因为,每条边代表一张牌,每个点代表一个数字,如果分量中边数多于点数则不能使每张牌都有对应的牌。
#include <cstdio> #include <iostream> #include <string.h> #include <stack> #include <vector> #include <queue> using namespace std; const int maxn=100005; int color[maxn],val[maxn],point[maxn],num,cnum; vector<int> v[maxn]; stack<int> st; struct Edge{ int from,to; }; Edge edge[50005]; void dfs(int now) { color[now]=cnum; point[cnum]++; for (int i=0;i<v[now].size();i++) { if (!color[v[now][i]]) dfs(v[now][i]); } } int main() { int n,q,t,i,j,x1,x2,y1,y2; scanf("%d",&t); for (q=1;q<=t;q++) { scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d%d",&x1,&y1); v[x1].push_back(y1); v[y1].push_back(x1); edge[i]=(Edge){x1,y1}; } num=cnum=0; memset(color,0,sizeof(color)); memset(point,0,sizeof(point)); for (i=1;i<=1e5;i++) { if (!color[i]) { cnum++; dfs(i); } } memset(val,0,sizeof(val)); for (i=1;i<=n;i++) { if (color[edge[i].from]==color[edge[i].to]) { val[color[edge[i].from]]++; } } int flag=1; for (i=1;i<=cnum;i++) { if (val[i]>point[i]) flag=0; } if (flag) printf("possible\n"); else printf("impossible\n"); for (i=1;i<=1e5;i++) v[i].clear(); } return 0; }
相关文章推荐
- 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】
- The 2011 Rocky Mountain Regional Contest&&Iterated Difference
- Nine(2012 Rocky Mountain Regional Contest)
- 2013 国庆第一场(The 2011 Rocky Mountain Regional Contest)
- The 2012 Rocky Mountain Regional Contest 题解
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
- 2015 ACM Amman Collegiate Programming Contest D.Alternating Strings【Dp】
- 2015-2016 Northwestern European Regional Contest (NWERC 2015) E. Elementary Math
- 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组
- Joy of Flight 2014-2015 ACM-ICPC, NEERC, Northern Subregional Contest
- I - Identifying Map Tiles 2015-2016 Northwestern European Regional Contest (NWERC 2015)
- HihoCoder 1249(2015 Asia Beijing Regional Contest)
- 2015 China Collegiate Programming Contest Nanyang
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest G题: Garden Gathering [线段树/最小曼哈顿距离生成树]
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
- 【codeforces】2014-2015 ACM-ICPC, Central Europe Regional Contest (CERC 14) F Vocabulary【dp】
- hdu5540 Secrete Master Plan(The 2015 China Collegiate Programming Contest )
- 2015 ACM Amman Collegiate Programming Contest
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)