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. vue-cli生成的模板各个文件详解(转)

    vue-cli脚手架中webpack配置基础文件详解 一.前言 原文:https://segmentfault.com/a/1190000014804826 vue-cli是构建vue单页应用的脚手架 ...

  2. [洛谷P3927]SAC E#1 - 一道中档题 Factorial

    题目大意:求$n!$在$k(k>1)$进制下末尾0的个数. 解题思路:一个数在十进制转k进制时,我们用短除法来做.容易发现,如果连续整除p个k,则末尾有p个0. 于是问题转化为$n!$能连续整除 ...

  3. RC Immix

    目录 RC Immix 目的 合并型引用计数 伪代码 优点和缺点 合并型引用计数法和Immix的融合 新对象 被动的碎片整理 积极的碎片整理 优点和缺点 优点 缺点 RC Immix Rifat Sh ...

  4. 在MAC上安装lxml到Python3

    首先可以直接使用以下命令安装lxml,但是会默认安装到Python2,没有找到怎么指定安装到Python3 sudo easy_install lxml 想要安装到Python3需要先安装pip: s ...

  5. 20180929 北京大学 人工智能实践:Tensorflow笔记01

    北京大学 人工智能实践:Tensorflow笔记 https://www.bilibili.com/video/av22530538/?p=13 (完)

  6. COGS——T1310. [HAOI2006]聪明的猴子

    http://cogs.pro/cogs/problem/problem.php?pid=1310 ★   输入文件:monkey.in   输出文件:monkey.out   简单对比时间限制:1 ...

  7. 【转载】C# 跨线程调用控件

    转自:http://www.cnblogs.com/TankXiao/p/3348292.html 感谢原作者,转载以备后用 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停 ...

  8. [PostCss] Easily Load Google Fonts with PostCSS Font Magician

    Configuring Google Fonts can be quite an annoying process to setup. Using Font Magician with PostCSS ...

  9. 基于ArcGIS Flex API实现动态标绘(1.2)

    动态标绘API 1.2,相较前一版本号(点击进入),该版本号新增对基本标绘符号的支持,包含: 单点.多点.折线.手绘线.多边形.手绘多边形.矩形,并提供对应的编辑功能. 例如以下图所看到的,对多点的编 ...

  10. OC07 -- 迭代器/NSNumber/NSValue/NSRange/NSSet/NSDate 及相互转换.(杂)

    //一: 迭代器 //数组 NSArray *arr=@[@"1",@"2",@"3",@"4",@"5&qu ...