您的位置:首页 > 其它

NOIP2010关押罪犯

2016-10-06 17:14 183 查看
题目来源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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: