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 ;
}

Gym - 100502A Amanda Lounges的更多相关文章

  1. Codeforces Gym100502A:Amanda Lounges(DFS染色)

    http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...

  2. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  3. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  4. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  5. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  6. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  7. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  8. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  9. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

随机推荐

  1. WSGI和CGI

    https://www.zhihu.com/question/19998865 https://segmentfault.com/a/1190000003069785

  2. iostat---监视磁盘CPU相关信息

    iostat命令被用于监视系统输入输出设备和CPU的使用情况.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.同vmstat一样,iostat也有一个弱点,就是它不能对某个进程进行深入分 ...

  3. 紫书 例题 10-7 UVa 10820 (欧拉函数)

    这道题要找二元组(x, y) 满足1 <= x, y <= n 且x与y互素 那么我就可以假设x < y, 设这时答案为f(n) 那么答案就为2 * f(n) +1(x与y反过来就乘 ...

  4. 雅礼集训1-9day爆零记

    雅礼集训1-9day爆零记 先膜一下虐爆我的JEFF巨佬 Day0 我也不知道我要去干嘛,就不想搞文化科 (文化太辣鸡了.jpg) 听李总说可以去看(羡慕)各路大佬谈笑风声,我就报一个名吧,没想到还真 ...

  5. vs2010和qt4.8.4配置

    最近项目要求在vs中开发qt程序,安装过后发现代码每天提示功能.由于本人记忆力有限,特在网上收罗了些配置方法. vs安装目录采用默认,qt安装目录:C:\Qt\4.8.4vs 在系统环境变量新建QTD ...

  6. 我看Java二十年:它是怎样永远改变编程的。

    转自jdon的小文章:来自Infoworld的一篇纪念mod=viewthread&tid=3042" target="_blank">Java诞生20周年 ...

  7. 一张图片让你了解android的事件分发机制

  8. Android Private Libraries 和 Dependencies的区别

    Android项目开发中,是不是常看到android-support-v4.jar.appcompat_v7.jar等的jar包? 至于为什么要添加这些包?添加有什么用?添加到哪里?相信很多人没过多关 ...

  9. JAVA并发-为现有的线程安全类添加原子方法

    JAVA中有许多线程安全的基础模块类,一般情况下,这些基础模块类能满足我们需要的所有操作,但更多时候,他们并不能满足我们所有的需要.此时,我们需要想办法在不破坏已有的线程安全类的基础上添加一个新的原子 ...

  10. .Net 断点调试

    设置断点,F11逐句查看,F10逐过程查看. 通过断点,可以查看程序走的过程,可以更清晰程序的流程. 通过断点,可以查看属性的值情况,判断哪里出了问题. 不过,只适用于pc端调试,浏览器访问才会触发. ...