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. 移动端(手机端)页面自适应解决方案—rem布局篇

    移动端(手机端)页面自适应解决方案-rem布局 假设设计妹妹给我们的设计稿尺寸为750 * 1340.结合网易.淘宝移动端首页html元素上的动态font-size属性.设计稿尺寸.前端与设计之间协作 ...

  2. PHP实时生成并下载超大数据量的EXCEL文件

    最近接到一个需求,通过选择的时间段导出对应的用户访问日志到excel中, 由于用户量较大,经常会有导出50万加数据的情况.而常用的PHPexcel包需要把所有数据拿到后才能生成excel, 在面对生成 ...

  3. 今日SGU 5.29

    sgu 299 题意:给你n个线段,然后问你能不能选出其中三个组成一个三角形,数字很大 收获:另一个大整数模板 那么考虑下为什么如果连续三个不可以的话,一定是不存在呢? 连续上个不合法的话,一定是 a ...

  4. UVALive 5790 Ball Stacking DP

    DP的方向真的很重要,这题做的时候死活想不出来,看了题解以后恍然大悟原来这么简单. 题意: 有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取.求选的球最大的权值和 ...

  5. Thrift 基础教程(一)安装篇

    1.Thrift简单介绍 Thrift是一款由Fackbook开发的可伸缩.跨语言的服务开发框架,该框架已经开源而且增加的Apache项目.Thrift主要功能是:通过自己定义的Interface D ...

  6. 分布式文件存储FastDFS(一)初识FastDFS

    一.FastDFS简单介绍 FastDFS是一款开源的.分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发.作为一个分布式文件系统,它对文件进行管理. ...

  7. RvmTranslator7.0-IFC

    RvmTranslator7.0-IFC eryar@163.com RvmTranslator can translate the RVM file exported by AVEVA Plant( ...

  8. shu_1171 十-&gt;二进制转换(输入输出控制)

    cid=1079&pid=19">http://202.121.199.212/JudgeOnline/problem.php?cid=1079&pid=19 分析:主 ...

  9. 15.C语言多线程实现变色龙以及cmd窗口标题变化

    #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h> #include <Wind ...

  10. SpringBoot 增加 拦截器 判断是否登录

    1.创建拦截器 package com.example.demo.interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactor ...