Time Limit: 1000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u

AMANDA AIR has routes between many different airports, and has asked their most important frequent flyers, members of the AA Frequent Flyer program, which routes they most often fly. Based on this survey, Amanda, the CEO and owner, has concluded that AMANDA AIR will place lounges at some of the airports at which they operate.

However, since there are so many routes going between a wide variety of airports, she has hired you to determine how many lounges she needs to build, if at all possible, given the constraints set by her. This calculation is to be provided by you, before any lounges are built. Her requirements specifies that for some routes, there must be lounges at both airports, for other H. A. Hansen, cc-by-sa routes, there must be lounges at exactly one of the airports, and for some routes, there will be no lounges at the airports.

She is very economically minded and is demanding the absolute minimum number of lounges to be built.

Input

The first line contains two non-negative integers 1 ≤ n,m ≤ 200 000, giving the number of airports and routes in the Amanda Catalog respectively. Thereafter follow m lines, each describing a route by three non-negative integers 1 ≤ a,b n and c ∈{0,1,2}, where a and b are the airports the route connects and c is the number of lounges.

No route connects any airport with itself, and for any two airports at most one requirement for that route is given. As one would expect, 0 is a request for no lounge, 1 for a lounge at exactly one of the two airports and 2 for lounges at both airports.

Output

If it is possible to satisfy the requirements, give the minimum number of lounges necessary to do so. If it is not possible, output impossible.

Sample Input 1       Sample Output 1

4 4

1 2

2 3

3 4

4 1

2

1

1

2

3

NCPC 2014 Problem A: Amanda Lounges

Sample Input 2 Sample Output 2

5 5

1 2

2 3

2 4

2 5

4 5

1

1

1

1

1

impossible

Sample Input 3 Sample Output 3

4 5

1 2

2 3

2 4

3 1

3 4

1

0

1

1

1

2

NCPC 2014 Problem A: Amanda Lounges

解题:二分图。。先把可以确定的点确定下来。2和0的都可以确定,然后再处理为1 的,当这些边中,其中已经处理了,那么既然是选一个,另一个也就确定了。

记得要进行传递。比如1 2 1已经把1选了,那么2就确定了,不选,此时还要去更新比如2 3 1啊,因为2 3之前没确定,2 是刚刚确定的,最后剩下的点是不知道选还是不选。

但是不管选和不选,起码不矛盾。

在不矛盾的情况下,看两种状态的哪种少,选少的状态用作选定状态,这样把保证选出的点数是最少 的!

被坑了一下午,傻逼就是傻逼啊。。。

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],color[maxn],n,m,tot,ans;
bool flag;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
queue<int>q;
void bfs(int u) {
int sum = ,o = ;
while(!q.empty()) q.pop();
q.push(u);
color[u] = ;
while(!q.empty()) {
u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(color[e[i].to] != -) {
if(color[e[i].to] == color[u]) {
flag = false;
return;
}
} else {
color[e[i].to] = color[u]?:;
q.push(e[i].to);
sum++;
if(color[e[i].to]) o++;
}
}
}
ans += min(o,sum-o);
}
void dfs(int u){
for(int i = head[u]; ~i; i = e[i].next){
if(color[e[i].to] == -){
color[e[i].to] = color[u]?:;
if(color[e[i].to] == ) ans++;
dfs(e[i].to);
}else if(color[e[i].to] == color[u]){
flag = false;
return;
}
}
}
int main() {
int u,v,w;
while(~scanf("%d %d",&n,&m)) {
memset(color,-,sizeof color);
memset(head,-,sizeof head);
flag = true;
ans = ;
for(int i = tot = ; i < m; ++i) {
scanf("%d %d %d",&u,&v,&w);
if(w == ) {
if(color[u] == || color[v] == ) flag = false;
else {
ans += color[u]!=;
ans += color[v]!=;
color[u] = color[v] = ;
}
} else if(w == ) {
if(color[u] == || color[v] == ) flag = false;
else color[u] = color[v] = ;
} else {
add(u,v);
add(v,u);
}
}
for(int i = ; i <= n&&flag; ++i) {
for(int j = head[i]; ~j && flag; j = e[j].next) {
if(e[j].to < i || color[i] == - && color[e[j].to] == -) continue;
if(color[i] != - && color[i] == color[e[j].to]) {
flag = false;
break;
}else dfs(color[i] == -?e[j].to:i);
}
}
for(int i = ; i <= n &&flag; ++i)
if(color[i] == -) bfs(i);
if(flag) printf("%d\n",ans);
else puts("impossible");
}
return ;
}

最新文章

  1. 深入理解 JavaScript 变量的作用域和作用域链
  2. 数据库 sql server
  3. Keepalived+Nginx架构整理版
  4. R语言-实用数据对象处理函数
  5. redis对比其余数据库
  6. apche的主配置文件)
  7. IOS基础之 (十二) Block
  8. log4j2.x 配置文件默认寻找顺序
  9. 在C#中子线程如何操作主线程中窗体上控件
  10. git plumbing 更加底层命令解析-深入理解GIT
  11. Book 最短路算法
  12. python 连接Mysql数据库
  13. java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils
  14. ubuntu 安装LaTex
  15. 更改EBSserver域名/IP
  16. Android 安装镜像
  17. 让SAE下的wordpress支持文件上传
  18. 【shell脚本练习】批量添加用户和设置密码
  19. PAT Basic 1005
  20. 4.清除cookie操作

热门文章

  1. 在angular4.X里使用mCustomScrollbar滚动条插件
  2. 洛谷1525 关押罪犯NOIP2010 并查集
  3. python IO编程-StringIO和BytesIO
  4. eclipse 启动报错has value &#39;1.7&#39;, but &#39;1.8&#39; is required
  5. 怎样安装Windows7操作系统
  6. Android GridView LruCache
  7. USACO2002 Open:雄伟的山峦
  8. Android Support Library 23.2用法简析
  9. GitHub开源控件的使用合集
  10. Python: PS 图层混合算法汇总