NOIP2010关押罪犯

题目来源https://www.luogu.org/problem/show?pid=1525


二分答案,再对此图进行二分图染色,只取长度>mid的边进行处理


保证长度>mid的边相邻的两个节点染上不同颜色


color数组表示颜色,0表示未染色


#include <algorithm>
#include <iostream>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cctype>
#include <vector>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=2e4+1;
const int inf=1e9;
struct node{int t,w;};
vector<node> a[maxn];
int color[maxn]={0};
bool inq[maxn]={0};
int main()
{
    ios::sync_with_stdio(false);
    int n,m;cin>>n>>m;
    int r=-inf,l=inf;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        node e;e.w=z;
        e.t=y;a[x].push_back(e);
        e.t=x;a[y].push_back(e);
        l=min(z,l);
        r=max(r,z);
    }
    if(l==r)l=0;
    while(l<r)
    {
        int mid=(l+r)/2;
        memset(color,0,sizeof(color));
        memset(inq,0,sizeof(inq));
        int ok=1;
        for(int i=1;i<=n;i++)
        {
            if(color[i]==0)
            {
                color[i]=1;
                queue<int> q;
                q.push(i);inq[i]=1;
                while(!q.empty())
                {
                    int u=q.front();q.pop();
                    for(int j=0;j<a[u].size();j++)
                    {
                        if(color[a[u][j].t]==0&&a[u][j].w>mid)
                        {
                            color[a[u][j].t]=3-color[u];
                            if(!inq[a[u][j].t])
                            {
                                q.push(a[u][j].t);
                                inq[a[u][j].t]=1;
                            }
                        }
                        else if(color[a[u][j].t]==color[u]&&a[u][j].w>mid)
                        {
                            ok=0;break;
                        }
                    }
                    if(!ok)break;
                }
                if(!ok)break;
            }
        }
        if(ok)r=mid;
        else l=mid+1;
    }
    cout<<l;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值