NOIP2010 Imprisoned criminals (graph theory + dichotomy)

Source: Internet
Author: User

At the time of the exam, the largest spanning tree was written, and then the binary image was stained, because such two contradictory criminals would not be divided into a prison.

The maximum spanning tree, however, timed out, 80 points.

Positive solution for two edge right, the edge weight is greater than the whole of the entire edge of the composition, to determine whether it is two, if not a binary graph, then no solution.

If there is no solution, then the restriction of the edge right is too small, because the edge too much, not easy to form a binary map, if there is a solution, then the limitations of the edge is too large, because fewer sides conducive to the formation of a binary map.

80-minute code, maximum spanning tree:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 20005using namespace Std;struct    t{int u;    int V;    int W; bool F;} A[100005];bool cmp (T x,t y) {return x.w > y.w;}    int fa[maxn];int Find (int x) {if (fa[x] = = x) return fa[x]; else return fa[x] = find (Fa[x]);}    struct e{int v;    int W;    int next; bool used;}    Edge[100005];int cnt,head[maxn];void add_edge (int u,int v,int w,bool flag) {edge[cnt].v = v;    EDGE[CNT].W = W;    edge[cnt].used = Flag;    Edge[cnt].next = Head[u]; Head[u] = cnt++;}    int n,m;int belong[maxn];int ans;bool vis[maxn];void dfs (int u,int FA)//Sub-graph dyeing + Edge selection max {Belong[u] = (belong[fa]+1)%2;    Vis[u] = 1;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; if (edge[i].used = = 0&&belong[v]! = -1&&belong[v] = = Belong[u]) {ans = max (ans,edge[i].w        );    } else if (edge[i].used = = 1&&!vis[v]) {DFS (v,u);    }}}int Main () {//freopen ("prison.in", "R", stdin);    Freopen ("Prison.out", "w", stdout);    memset (head,-1,sizeof head);    scanf ("%d%d", &n,&m);        for (int i = 0; i < m; i++) {int x, y, Z;        scanf ("%d%d%d", &x,&y,&z);        A[I].U = x;        A[I].V = y;        A[I].W = Z;    A[I].F = 0;    } sort (a,a+m,cmp);    for (int i = 0; I <= N; i++) fa[i] = i;        for (int i = 0; i < m; i++)//maximum spanning tree {int fx = find (A[I].U);        int fy = find (A[I].V);            if (FX! = FY) {a[i].f = 1;            FA[FX] = FY;            Add_edge (a[i].u,a[i].v,a[i].w,1);        Add_edge (a[i].v,a[i].u,a[i].w,1); }} for (int i = 0; i < m; i++)//unused edges, if the edges of the two points are the same side and the two points are dyed the same color, then they belong to a prison, so the answer is the maximum edge right {if (A[i]) that meets the above conditions            . F = = 0) {Add_edge (a[i].u,a[i].v,a[i].w,0);        Add_edge (a[i].v,a[i].u,a[i].w,0);    }} memset (belong,-1,sizeof belong); for (int i = 1; I <= N; i++) {if (belong[i] = =-1) DFS (i,0);    } printf ("%d\n", ans); return 0;}

Full out code, two points:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define MAXN 20005using namespace Std;struct t{int v;int next;} Edge[100005];int head[maxn],cnt;void add_edge (int u,int v) {edge[cnt].v = V;edge[cnt].next = Head[u];head[u] = cnt++;} struct E{int u;int v;int w;bool operator < (const E &x) Const{return w > x.w;}}  A[100005];int n,m;int color[maxn];bool flag;bool dfs (int u)//dichotomy coloring {for (int i = head[u]; I! =-1; i = edge[i].next) {int v = Edge[i].v;if (color[v] = = Color[u]) {return false;} else if (color[v] = = 1) {Color[v] = (color[u]+1)%2;if (dfs (v) = = false) return false;}} return true;} BOOL Check (int limit) {cnt = 0;memset (head,-1,sizeof head); memset (color,-1,sizeof color); memset (edge,0,sizeof edge); for (int i = 0; i < m; i++)//The full edge (A[I].W <= limit) Break;add_edge (A[I].U,A[I].V) that has a margin greater than the limiting condition; Add_edge (a[i].v,a[i). u);} for (int i = 1; I <= n; i++) {if (color[i] = = 1) {Color[i] = 0;if (DFS (i) = = false) return false;//staining unsuccessful, limit too small}}return tr UE;} InchT Main () {scanf ("%d%d", &n,&m), int l,r,mid;for (int i = 0; i < m; i++) scanf ("%d%d%d", &a[i].u,&a[i].v,& AMP;A[I].W), r = Max (R,A[I].W); sort (a,a+m);//Whether the order here is not much affected by the result L = -1;//Note the boundary condition while (l+1<r) {mid = (l+r)/2;if (check ( MID)) R = Mid;else L = mid;} printf ("%d\n", r);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NOIP2010 Imprisoned criminals (graph theory + dichotomy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.