洛谷P1330封锁阳关大学口胡实况

原题目见洛谷(https://www.luogu.org/problemnew/show/P1330
这道题如果仔细想想的话其实河蟹的分布就是对这张图进行黑白染色,且相邻的两点要染成不同的颜色,如果方案可行那么染色的两种方案中,颜色少的就是所需要的,基本思路的话就是这样

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define R register 
#define maxm 200000
#define maxn 20000
using namespace std;

struct edge{
    int t,nexty;
}edge[maxm];
int head[maxn],cnt=0;
bool used[maxn]={0};
int col[maxn]={0},sum[2];

inline int read(){
    int num=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9'){
        if(ch=='-'){
            f=-1;
        }
        ch=getchar();
    }
    while(ch>='0' && ch<='9'){
        num=num*10+ch-'0',ch=getchar();
    }
    return num*f;
}
inline void out(int x){
    if(x>=10){
        out(x/10);
    }
    putchar(x%10+'0');
}
inline void add(int a,int b){
    ++cnt,edge[cnt].t=b,edge[cnt].nexty=head[a],head[a]=cnt;
}
inline bool dfs(int node,int color){
    if(used[node]){
        if(col[node]==color){
            return true;
        }
        return false;
    }
    used[node]=true,++sum[col[node]=color];
    bool judge=true;
    for(R int i=head[node];i!=0 && judge;i=edge[i].nexty){
        judge=judge && dfs(edge[i].t,1-color);
    }
    return judge;
}
inline int _min(int a,int b){
    return a>b?b:a;
}

int main(){
    int n=read(),m=read();
    int a,b;
    while(m--){
        a=read(),b=read(),add(a,b),add(b,a);
    }
    int ans=0;
    for(R int i=1;i<=n;++i){
        if(used[i]){
            continue;
        }
        memset(sum,0,sizeof(sum));
        if(!dfs(i,0)){
            printf("Impossible");
            return 0;
        }
        ans+=_min(sum[0],sum[1]);
    }
    out(ans);
    return 0;
}

代码跑了12ms,但是居然还有跑0ms!!!,不过还很快吧qwq

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值