[NOIP2010]看押罪犯(二分+二分图染色)

www.MyException.Cn  网友分享于：2015-08-17  浏览：0次
[NOIP2010]关押罪犯(二分+二分图染色)

``````#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAXM 100005
#define MAXN 20005
using namespace std;
int col[MAXN], n, m;
struct node
{
int v, w;
node *next;
}Edge[MAXM * 2], *Adj[MAXN], *Mcnt = Edge;
struct E
{
int u, v, w;
bool operator < (const E rhs) const
{
return w < rhs.w;
}
}e[MAXM];
void Addedge(int u, int v, int w)
{
node *t = ++Mcnt;
t->v = v;
t->w = w;
}
int ans, mid;
bool dfs(int u, int fa)
{
if(col[u] != -1 && col[u] == col[fa]) return 0;
if(col[u] == !col[fa]) return 1;
col[u] = !col[fa];
for(node *p = Adj[u]; p; p = p->next)
if(p->v != fa)
if(p->w > e[mid].w)
if(!dfs(p->v, u))
return 0;
return 1;
}
int main()
{
scanf("%d%d", &n, &m);
int l = 0, r = m;
for(int i = 1; i <= m; i ++)
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
sort(e + 1, e + m + 1);
for(int i = 1; i <= m; i ++)
{
}
while(l <= r)
{
mid = (l + r) >> 1;
memset(col, -1, sizeof(int) * (n+2));
col[0] = 0;
bool flag = 0;
for(int i = 1; i <= n; i ++)
if(col[i] == -1)
if(!dfs(i, 0))
{
flag = 1;
break;
}
if(flag) l = mid + 1;
else {ans = e[mid].w; r = mid - 1;}
}
printf("%d\n", ans);
return 0;
}``````