zoukankan      html  css  js  c++  java
  • Gym

    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 是刚刚确定的,最后剩下的点是不知道选还是不选。

    但是不管选和不选,起码不矛盾。

    在不矛盾的情况下,看两种状态的哪种少,选少的状态用作选定状态,这样把保证选出的点数是最少 的!

     

    被坑了一下午,傻逼就是傻逼啊。。。

     

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 200010;
     4 struct arc {
     5     int to,next;
     6     arc(int x = 0,int y = -1) {
     7         to = x;
     8         next = y;
     9     }
    10 } e[maxn<<1];
    11 int head[maxn],color[maxn],n,m,tot,ans;
    12 bool flag;
    13 void add(int u,int v) {
    14     e[tot] = arc(v,head[u]);
    15     head[u] = tot++;
    16 }
    17 queue<int>q;
    18 void bfs(int u) {
    19     int sum = 1,o = 0;
    20     while(!q.empty()) q.pop();
    21     q.push(u);
    22     color[u] = 0;
    23     while(!q.empty()) {
    24         u = q.front();
    25         q.pop();
    26         for(int i = head[u]; ~i; i = e[i].next) {
    27             if(color[e[i].to] != -1) {
    28                 if(color[e[i].to] == color[u]) {
    29                     flag = false;
    30                     return;
    31                 }
    32             } else {
    33                 color[e[i].to] = color[u]?0:2;
    34                 q.push(e[i].to);
    35                 sum++;
    36                 if(color[e[i].to]) o++;
    37             }
    38         }
    39     }
    40     ans += min(o,sum-o);
    41 }
    42 void dfs(int u){
    43     for(int i = head[u]; ~i; i = e[i].next){
    44         if(color[e[i].to] == -1){
    45             color[e[i].to] = color[u]?0:2;
    46             if(color[e[i].to] == 2) ans++;
    47             dfs(e[i].to);
    48         }else if(color[e[i].to] == color[u]){
    49             flag = false;
    50             return;
    51         }
    52     }
    53 }
    54 int main() {
    55     int u,v,w;
    56     while(~scanf("%d %d",&n,&m)) {
    57         memset(color,-1,sizeof color);
    58         memset(head,-1,sizeof head);
    59         flag = true;
    60         ans = 0;
    61         for(int i = tot = 0; i < m; ++i) {
    62             scanf("%d %d %d",&u,&v,&w);
    63             if(w == 2) {
    64                 if(color[u] == 0 || color[v] == 0) flag = false;
    65                 else {
    66                     ans += color[u]!=2;
    67                     ans += color[v]!=2;
    68                     color[u] = color[v] = 2;
    69                 }
    70             } else if(w == 0) {
    71                 if(color[u] == 2 || color[v] == 2) flag = false;
    72                 else color[u] = color[v] = 0;
    73             } else {
    74                 add(u,v);
    75                 add(v,u);
    76             }
    77         }
    78         for(int i = 1; i <= n&&flag; ++i) {
    79             for(int j = head[i]; ~j && flag; j = e[j].next) {
    80                 if(e[j].to < i || color[i] == -1 && color[e[j].to] == -1) continue;
    81                 if(color[i] != -1 && color[i] == color[e[j].to]) {
    82                     flag = false;
    83                     break;
    84                 }else dfs(color[i] == -1?e[j].to:i);
    85             }
    86         }
    87         for(int i = 1; i <= n &&flag; ++i)
    88             if(color[i] == -1) bfs(i);
    89         if(flag) printf("%d
    ",ans);
    90         else puts("impossible");
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    MCMC等采样算法
    【模拟退火】旅行商问题
    团队项目博客汇总
    2018年春季学期-助教总结
    linux简单命令常用随记
    记一次计算机网络作业
    [东北师大软工]Week2-作业2:个人项目实战 初步测试结果
    为什么你学过Java却忘光了——记第一次助教同学见面会
    ahk打印成pdf记录
    PSP总结报告
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4439580.html
Copyright © 2011-2022 走看看