CodeForces 741C Arpa’s overnight party and Mehrdad’s silent entering
2016-12-09 12:18
417 查看
题目大意
有2n个人坐成一圈,给定n对情侣,男的在a,女的在b。现在有两种食物分配给这些人,要求每一对情侣分得食物不同,还要求任意连续三人至少有两种食物。
求分配方案。
Data Constraint
n≤105
题解
先每队情侣之间连双向边。1→2,3→4,5→6...之间连双向边。
每条边表示这两个位置分到的食物不同。
连完边后染一遍色。
正确性:每次连边的时候环的长度必然是偶数,所以是一定有解的。
时间复杂度:O(n)
SRC
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std ; #define N 100000 + 10 #define M 10000000 + 10 struct Couple { int a , b ; } P ; bool flag = 1 ; int Node[M] , Next[M] , Head[M] , tot ; int Col[M] , D[M] ; int n ; void link( int u , int v ) { Node[++tot] = v ; Next[tot] = Head[u] ; Head[u] = tot ; } void Insert( int u , int v ) { link(u,v) , link(v,u) ; } void BFS( int st ) { int i = 0 , j = 1 ; D[1] = st ; Col[st] = 1 ; while ( i < j ) { i ++ ; int now = D[i] ; for (int p = Head[now] ; p ; p = Next[p] ) { if ( Col[Node[p]] && Col[Node[p]] != 3 - Col[now] ) { flag = 0 ; return ; } if ( Col[Node[p]] ) continue ; Col[Node[p]] = 3 - Col[now] ; D[++j] = Node[p] ; } } } int main() { scanf( "%d" , &n ) ; for (int i = 1 ; i <= n ; i ++ ) { scanf( "%d%d" , &P[i].a , &P[i].b ) ; Insert( P[i].a , P[i].b ) ; } for (int i = 1 ; i <= 2 * n ; i += 2 ) Insert( i , i + 1 ) ; for (int i = 1 ; i <= 2 * n && flag ; i ++ ) { if ( Col[i] ) continue ; BFS(i) ; } if ( !flag ) { printf( "-1\n" ) ; return 0 ; } for (int i = 1 ; i <= n ; i ++ ) { printf( "%d %d\n" , Col[P[i].a] , Col[P[i].b] ) ; } return 0 ; }
以上.
相关文章推荐
- 二分图(Arpa’s overnight party and Mehrdad’s silent entering,cf 741C)
- Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering(贪心,二分图)
- Codeforces Round #383 (Div. 2)E. Arpa’s overnight party and Mehrdad’s silent entering【二分图染色】
- codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
- Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
- 【CF741C】Arpa's overnight party and Mehrdad's silent entering 题解
- 二分图染色——Codeforces741C Arpa's overnight party and Mehrdad's silent entering
- codeforces741C Arpa’s overnight party and Mehrdad’s silent entering(二分图)
- Codeforces C. Arpa’s overnight party and Mehrdad’s silent entering
- Codeforces 742C C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces 742A-Arpa’s hard exam and Mehrdad’s naive cheat(快速幂)
- codeforcesB - Arpa’s obvious problem and Mehrdad’s terrible solution 【思维】
- codeforces 742B - Arpa’s obvious problem and Mehrdad’s terrible solution
- codeforces 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths (dsu on the tree)
- codeforces 742d D - Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Codeforces 742B-Arpa’s obvious problem and Mehrdad’s terrible solution
- 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
- Codeforces 741A-Arpa's loud Owf and Mehrdad's evil plan
- Arpa’s obvious problem and Mehrdad’s terrible solution (CodeForces - 742B)