您的位置:首页 > 其它

hdu 1814

2016-08-17 20:09 399 查看
Time Limit:5000MS    Memory Limit:32768KB    64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeHDU
1814

Appoint description:

Description

The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

The Commission has to fulfill the following conditions:

1.Each party has exactly one representative in the Commission,

2.If two deputies do not like each other, they cannot both belong to the Commission.

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

Task

Write a program, which:

1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,

2.decides whether it is possible to establish the Commission, and if so, proposes the list of members,

3.writes the result in the text file SPO.OUT.

Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In
each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.

There are multiple test cases. Process to end of file.

Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval
from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence.

Sample Input

3 2
1 3
2 4


Sample Output

1
4
5


暴力即可

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
typedef long long ll;
void fre()
{
freopen("alex.txt", "r", stdin);
freopen("alex.txt", "w", stdout);
}
inline void in(int &x)
{
register char ch;
while (ch = getchar(), (ch < '0' || ch > '9'));
x = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - '0';
}
inline void out(int x)
{
register char hc[30];
register int len = 0;
hc[len++] = x % 10 + '0';
while (x /= 10) hc[len++] = x % 10 + '0';
for (int i = len - 1; i >= 0; i--) putchar(hc[i]);
}
const int maxn = 20005;
struct node
{
int from, to, next;
}edge[maxn * 4];
int hand[maxn], belong[maxn], dfn[maxn], low[maxn], vis[maxn], st[maxn];
int fp[maxn];//建立对应SCC编号的映射
int color[maxn];//染色
int In[maxn];//新图SCC的入度
vector<int>G[maxn];//缩点后新图
int tot, num, index, cnt, n, m;
void addedge(int a, int b)
{
edge[tot].from = a;
edge[tot].to = b;
edge[tot].next = hand[a];
hand[a] = tot++;
}
void init()
{
memset(hand, -1, sizeof(hand));
memset(belong, -1, sizeof(belong));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(vis, 0, sizeof(vis));
memset(st, 0, sizeof(st));
tot = 0;
num = 0;
index = 0;
cnt = 0;
}
void Tarjan(int u)
{
dfn[u] = low[u] = index++;
vis[u] = 1;
st[num++] = u;
for (int i = hand[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u])
{
cnt++;
int v;
do
{
v = st[--num];
vis[v] = 0;
belong[v] = cnt;
} while (u != v);
}
}

int ans[maxn], step;
int dfs(int u)
{
if (color[u] == 2)
return 0;
if (color[u] == 1)
return 1;
color[u] = 1;
color[u ^ 1] = 2;
ans[step++] = u;
for (int i = hand[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!dfs(v))
return 0;
}
return 1;

}
int solve()
{
int i, j;
memset(color, 0, sizeof(color));
for (i = 0; i < 2 * n; ++i)
{
if (color[i])
continue;
step = 0;
if (!dfs(i))
{
for (int j = 0; j < step; ++j)
{
color[ans[j]] = 0;
color[ans[j] ^ 1] = 0;
}
if (!dfs(i ^ 1))
return 0;
}
}
return 1;
}
int main()
{

int i, j;
while (~scanf("%d%d", &n, &m))
{
init();
for (i = 1; i <= m; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
addedge(a, b ^ 1);
addedge(b, a ^ 1);
}
/*for (i = 0; i < 2 * n; ++i)
{
if (!dfn[i])
Tarjan(i);
}*/
if (solve())
{
for (int i = 0; i < 2 * n; i++)
{
if (color[i] == 1)
printf("%d\n", i + 1);
}
}
else
printf("NIE\n");

}
}
/*
5 5
1 3
2 4
3 6
5 7
6 8
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: