Amanda Lounges

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 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 Format

The first line contains two non-negative integers 1 \le n, m \le 200 0001≤n,m≤200000, giving the number of airports and routes in the Amanda Catalog respectively. Thereafter follow mm lines, each describing a route by three non-negative integers 1 \le a, b \le n1≤a,b≤n and c \in \{0, 1, 2\}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, 00 is a request for no lounge, 11 for a lounge at exactly one of the two airports and 22for lounges at both airports.

Output Format

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".

样例输入1复制

4 4
1 2 2
2 3 1
3 4 1
4 1 2

样例输出1复制

3

样例输入2复制

5 5 
1 2 1 
2 3 1 
2 4 1 
2 5 1 
4 5 1

样例输出2复制

impossible

样例输入3复制

4 5 
1 2 1 
2 3 0 
2 4 1 
3 1 1 
3 4 1

样例输出3复制

2

题目来源

Nordic Collegiate Programming Contest 2014

#include <bits/stdc++.h>
using namespace std;
#define inf 99999999
int Scan()
{   //  输入外挂  
    int res = 0, flag = 0;  
    char ch;  
    if ((ch = getchar()) == '-') 
    {   
        flag = 1;  
    }    
    else if(ch >= '0' && ch <= '9') 
    {
        res = ch - '0'; 
    }
    while ((ch = getchar()) >= '0' && ch <= '9')  
    {
        res = res * 10 + (ch - '0');  
    }
    return flag ? -res : res;  
}  
int a[200010];
int b[200010];
int vis[200010];
vector<int>we[200010];
int dfs(int s){
	int sum=0;
	queue<int>q;
	queue<int>w;
	w.push(s);
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		sum+=a[u];
		q.pop();
		for(int i=0;i<we[u].size();i++){
			int v=we[u][i];
			if(vis[v]){
				if(a[u]+a[v]!=1)
				return inf; 
			}
			else{
				vis[v]=1;
				b[v]=1;
				a[v]=1-a[u];
				q.push(v);
				w.push(v);
			}
		}
	}
	while(!w.empty()){
		int s=w.front();
		vis[s]=0;
		w.pop();
	}
	return sum;
} 
int main(){
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		for(int i=0;i<=n;i++){
			vis[i]=0;
			a[i]=0;
			b[i]=0;
			we[i].clear();
		}
		int x,y,w;
		int f=0;
		int sum=0;
		for(int i=0;i<m;i++){
			//scanf("%d %d %d",&x,&y,&w);
			x=Scan();
			y=Scan();
			w=Scan();
			if(f==1){
				continue;
			}
			if(w==2){
				for(int j=0;j<we[x].size();j++){
					if(a[we[x][j]]==1){
						f=1;
						break;
					}
				}
				for(int j=0;j<we[y].size();j++){
					if(a[we[y][j]]==1){
						f=1;
						break;
					}
				}
				if(f==1){
					continue;
				}
				if(vis[x] && a[x]==0){
					f=1;
					continue;
				}
				else{
					if(vis[x]==0){
						sum++;
						vis[x]=1;
						b[x]=1;
					}
					a[x]=1;
				}
				if(vis[y] && a[y]==0){
					f=1;
					continue;
				}
				else{
					if(vis[y]==0){
						sum++;
						vis[y]=1;
						b[y]=1;
					}
					a[y]=1;
				}
			}
			else if(w==0){
				if(vis[x] && a[x]==1){
					f=1;
					continue;
				}
				else{
					vis[x]=1;
					b[x]=1;
					a[x]=0;
				}
				if(vis[y] && a[y]==1){
					f=1;
					continue;
				}
				else{
					vis[y]=1;
					b[y]=1;
					a[y]=0;
				}
			}
			else{
				if(a[x]==1 && a[y]==1){
					f=1;
					continue;
				}
				else if(a[x]==1){
					vis[y]=1;
					b[y]=1;
					a[y]=0;
				}
				else if(a[y]==1){
					vis[x]=1;
					b[x]=1;
					a[x]=0;
				}
				else{
					if(vis[x] && vis[y]){
						f=1;
						continue;
					}
					else if(vis[x]){
						sum++;
						vis[y]=1;
						b[y]=1;
						a[y]=1;
					}
					else if(vis[y]){
						sum++;
						vis[x]=1;
						b[x]=1;
						a[x]=1;
					}
					else{
						we[x].push_back(y);
						we[y].push_back(x);
					}
				}
			} 
		}
		if(f==1){
			printf("impossible\n");
			continue;
		}
		for(int i=1;i<=n;i++){
			if(b[i]==0 && we[i].size()!=0){
				int w=inf;
				vis[i]=1;
				a[i]=1;
				w=min(w,dfs(i));
				a[i]=0;
				w=min(dfs(i),w);
				if(w==inf){
					f=1;
					break;
				}
				sum+=w;
			}
		}
		if(f==1){
			printf("impossible\n");
		}
		else{
			printf("%d\n",sum);
		}
	}
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值