HD1814Peaceful Commission(模板题)

题目链接

题意:

和平委员会

根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立。 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍。

此委员会必须满足下列条件:

  • 每个党派都在委员会中恰有1个代表,
  • 如果2个代表彼此厌恶,则他们不能都属于委员会。

每个党在议会中有2个代表。代表从1编号到2n。 编号为2i-1和2i的代表属于第I个党派。

任务

写一程序:

  • 从文本文件读入党派的数量和关系不友好的代表对,
  • 计算决定建立和平委员会是否可能,若行,则列出委员会的成员表,
  • 结果写入文本文件。

输入

在文本文件的第一个行有2非负整数n和m。 他们各自表示:党派的数量n,1 < =n < =8000和不友好的代表对m,0 <=m <=20000。 在下面m行的每行为一对整数a,b,1<=a <b<=2n,中间用单个空格隔开。 它们表示代表a,b互相厌恶。

输出

如果委员会不能创立,文本文件中应该包括单词NIE。若能够成立,文本文件SPO.OUT中应该包括n个从区间1到2n选出的整数,按升序写出,每行一个,这些数字为委员会中代表的编号。如果委员会能以多种方法形成,程序可以只写他们的某一个。

样品输入

3 2
1 3
2 4

样品输出

1
4
5分析:对于某个党两个代表只能选择一个,同时在厌恶的关系中的两个人只能有一个人存在,

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 const int Maxn = 8000 * 2 + 10;
 7 const int Maxm = 20000 * 2 + 10; //注意数据范围
 8 struct Edge
 9 {
10     int to, Next;
11 }edge[Maxm];
12 int head[Maxn], tot;
13 void init()
14 {
15     tot = 0;
16     memset(head, -1, sizeof(head));
17 }
18 void addedge(int u, int v)
19 {
20     edge[tot].to = v;
21     edge[tot].Next = head[u];
22     head[u] = tot++;
23 }
24 bool vis[Maxn];
25 int S[Maxn], top;
26 bool dfs(int u)
27 {
28     if (vis[u ^ 1])
29         return  false;
30     if (vis[u])
31         return true;
32     vis[u] = true;
33     S[top++] = u;
34     for (int i = head[u]; i != -1; i = edge[i].Next)
35         if (!dfs(edge[i].to))
36             return false;
37     return true;
38 }
39 bool Twosat(int n)
40 {
41     memset(vis, 0, sizeof(vis));
42     for (int i = 0; i < n; i += 2)
43     {
44         if (vis[i] || vis[i ^ 1]) //已经选择了继续
45             continue;
46         top = 0;
47         if (!dfs(i))
48         {
49             while (top)
50                 vis[ S[--top] ] = false;
51             if (!dfs(i ^ 1))
52                 return false;
53         }
54     }
55     return true;
56 }
57 int main()
58 {
59     int n, m;
60     int u, v;
61     while (scanf("%d%d", &n, &m) != EOF)
62     {
63         init();
64         while (m--)
65         {
66             scanf("%d%d", &u, &v);
67             u--;
68             v--;
69             addedge(u, v ^ 1);
70             addedge(v, u ^ 1);
71         }
72         if (Twosat(n * 2))
73         {
74             for (int i = 0; i < 2 * n; i++)
75                 if (vis[i])
76                     printf("%d\n", i + 1);
77         }
78         else printf("NIE\n");
79     }
80
81     return 0;
82 }

时间: 2024-07-30 16:04:25

HD1814Peaceful Commission(模板题)的相关文章

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q