zoukankan      html  css  js  c++  java
  • 【Uva 11080】Place the Guards

    Link:

    Description

    一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫.

    Solution

    二分图;
    如果某个连通块不能形成二分图->不行!
    如果能形成二分图.
    则看看是在染成颜色0的位置放守卫还是在1的位置放守卫;
    如果连通块只有1个的话,答案是1而不是0;
    因为有说所有的点也要覆盖到的。
    这题和最小点覆盖不同,最小点覆盖的话,两个取的点之间可以有边相连
    最小点覆盖也没有涉及到单个的点的处理。

    NumberOf WA

    0

    Reviw


    Code

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define ri(x) scanf("%d",&x)
    #define rl(x) scanf("%lld",&x)
    #define rs(x) scanf("%s",x+1)
    #define oi(x) printf("%d",x)
    #define ol(x) printf("%lld",x)
    #define oc putchar(' ')
    #define os(x) printf(x)
    #define all(x) x.begin(),x.end()
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 200;
    
    int n,m,color[N+20],cnt[2],ans;
    vector <int> G[N+20];
    
    int dfs(int x,int c){
        color[x] = c;
        cnt[c]++;
        int len = G[x].size(),bo = 1;
        rep1(i,0,len-1){
            int y = G[x][i];
            if (color[y]==-1)
                bo &= dfs(y,1-c);
            else
                if (color[y]==color[x])
                    return 0;
        }
        return bo;
    }
    
    int main(){
        //Open();
        //Close();
        int T;
        ri(T);
        while (T--){
            rep1(i,1,N) G[i].clear();
            ri(n),ri(m);
            rep1(i,1,m){
                int x,y;
                ri(x),ri(y);
                x++,y++;
                G[x].pb(y),G[y].pb(x);
            }
    
            ans = 0;
            int ok = 1;
            ms(color,255);
            rep1(i,1,n)
                if (color[i]==-1){
                    cnt[0] = 0,cnt[1] = 0;
                    ok &= dfs(i,0);
                    if (cnt[0] + cnt[1] == 1){
                        ans+=1;
                    }else
                        ans+=min(cnt[0],cnt[1]);
                }
            if (!ok)
                puts("-1");
            else
                oi(ans),puts("");
        }
        return 0;
    }
  • 相关阅读:
    排序算法之冒泡排序的思想以及Java实现
    排序算法之希尔排序的思想以及Java实现
    c# npoi分批往excel追加数据
    c# Timer按月执行任务
    windows服务+定时任务(quartz.net)+nancy
    c# linq分组 lambda分组
    批量插入sql技巧
    解决windows server 2008R2自动关机
    c# DataGridView在使用DataSource时,只显示指定的列或禁止自动生成列
    c# mongodb时间类型字段保存时相差八个小时解决办法
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626118.html
Copyright © 2011-2022 走看看