Gym - 100502A Amanda Lounges
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的更多相关文章
- Codeforces Gym100502A:Amanda Lounges(DFS染色)
http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
随机推荐
- MongoDB 的replicattion 复制集练习
replicattion 相当于 mysql 的主从复制的读写分离,共同维护相同的数据,提高服务器的可用性[假如主(PRIMARY)不能用时,mongo会迅速自动切到从(SECON ...
- NodeJS学习笔记 进阶 (4)基于express+muter的文件上传(ok)
个人总结:这篇文章主要讲了multer插件的使用,类似于formidable,可以用来处理post表单中的文件上传,读完这篇文章需要10分钟. 摘选自网络 概览 图片上传是web开发中经常用到的功能, ...
- [POI2009]KON-Ticket Inspector(二维前缀和+DP)
题意 有n个车站,现在有一辆火车从1到n驶过,给出aij代表从i站上车j站下车的人的个数.列车行驶过程中你有K次检票机会,所有当前在车上的人会被检票,问最多能检多少个不同的人的票 (n<=600 ...
- 如何解决本地仓库和远程仓库的冲突(Conflict)
Background: 我有一个github仓库管理我的代码,我将这个仓库的代码clone到我的工作电脑和私人电脑本地方便我上班和在家时都可以对我的代码进行更新. 一天,我在家修改过代码之后并未提交, ...
- Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...
- 在Maven项目中关于SSM框架中邮箱验证登陆
1.你如果要在maven项目中进行邮箱邮箱验证,你首先要先到pom.xml文件中配置mail.jar,activation.jar包 <dependency> <groupId> ...
- how to backup and restore database of SQL Server
Back up 1,右键选中需要备份的数据库,Tasks-->Backup 2.General中,Destination,先remove掉之前的,然后再Add 需要注意的是,add的文件,必须要 ...
- Visual Code的调试
Run 'Debug: Download .NET Core Debugger' in the Command Palette or open a .NET project directory to ...
- Lesson 2 Building your first web page: Part 1
In this ‘hands-on’ module we will be building our first web page in no time. We just need to quickly ...
- 在IIS中给某一个网站添加binding的坑
以http为例, 假如ip地址从局域网的地址172.31.212.20改为127.0.0.1 但是网站没法访问了http://172.31.212.20/chile.backoffice/ 必须通过 ...