CSU 1574 Amanda Lounges Simulation problem

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

A graph of the n-point m edges (starting at each point is white)

The following M-line gives the edges and edges, and the edge weights indicate the number of dots that are dyed black in the 2 points connected by the edge.

0 means dye, 1 means one dianran, and 2 means dye.

Q: The minimum number of points to be dyed can meet the above edge rights. If there is no output impossible

The case of all edge weights 0 and 2 is processed first.

In this case, only the sub-graph with the edge weight of 1 is left.

Dye a point, and then the BFS to dye the map.


#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include <math.h > #include <vector>using namespace std;template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (C=ge TCHAR (), c==eof) return 0;while (c!= '-' && (c< ' 0 ' | | C> ' 9 ')) C=getchar (), sgn= (c== '-'), -1:1;ret= (c== '-')? 0: (c ' 0 '); while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '); Ret*=sgn;return 1;}        Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-');    x =-X;    } if (X>9) pt (X/10); Putchar (x%10+ ' 0 ');}  const int N = 200105;const int M = n<<1;typedef pair<int,int> pii;vector<pii>g;struct Edge{int from, to, Col, NEX;} edge[m];//Note N M to modify int head[n], edgenum;void Add (int u, int v, int col) {Edge E = {u, V, col, head[u]};edge[edgenum] = e;h Ead[u] = Edgenum + +;} void Init () {memset (head,-1, sizeof head); edgenum = 0;} int C[n], ans, N, m;int bfs (int x) {int siz = 1, ran = 1; c[x] = 1;queue<int>q;q.push (x);//printf ("bfs:%d ' s color =%d\n", x, C[x]), while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = Head[u ]; ~i; i = Edge[i].nex) {int v = edge[i].to;//printf ("Bfs_edge (%d,%d)-(%d,%d) \ n", u, C[u], V, C[v]), if (c[v]!=-1) {//printf ("sum = %d\n ", c[u]+c[v]); if ((C[u]+c[v))!=1) return-1;} else {Siz++;c[v] = C[u]^1;ran + = C[v];q.push (v);//printf ("%d s color =%d\n", V, C[v]);}} Return min (ran, Siz-ran);} BOOL Solve () {int u, V, D;memset (c,-1, sizeof C), for (int i = 0; i < m; i++) {rd (U); Rd (v); Rd (d); Add (U, V, d); add (V, U, d);} queue<int>q;for (int i = 0; i < Edgenum; i+=2) {u = edge[i].from; v = edge[i].to; d = edge[i].col;if (d = = 0) {if (c[u ]==1 | | C[V] = = 1) return false;if (c[u]==-1) Q.push (U), c[u] = 0;if (c[v]==-1) Q.push (v), c[v] = 0;} else if (d = = 2) {if (c[u] = = 0 | | c[v] = = 0) return false;if (c[u]==-1) Q.push (U), c[u] = 1;if (c[v]==-1) Q.push (v), c[v] = 1;}} while (!q.empty ()) {u = Q.front (), Q.pop (), for (int i = Head[u];~i;i=edge[i].nex) {v = edge[i].to;if (c[v]!=-1) {if (Edge[i]. Col! = C[u]+c[v]) return false;} else {C[v] = C[u]^1;q.push (v);}}} Puts ("init:"); for (int i = 1; I <= N; i++) printf ("(%d ' color is%d) \ n", I, C[i]); for (int i = 1; I <= n; i++) ans + = c[i] = = 1; G.clear (); for (int i = 0; i < Edgenum; i+=2) {if (Edge[i].col! = 1) continue;u = edge[i].from; v = edge[i].to;if (C[u]==-1 & amp;& c[v]==-1) G.push_back (PII (U,v));}  Init (); for (int i = 0; i < (int) g.size (); i++) {u = g[i].first; v = g[i].second; add (u,v,1); add (v,u,1);//printf ("Addedge (%d,%d) \ n ", u, v);} for (int i = 1; I <= n; i++) if (c[i] = = 1) {int tmp = BFS (i); if (tmp = =-1) return False;ans + = tmp; return true;} int main () {while (~SCANF ("%d%d", &n, &m)) {init (); ans = 0;if (false = = Solve ()) puts ("impossible"); else cout< <ans<<endl;} return 0;} /*5 2 (3) 4 5 1ans:35 2 3 4 5 5 2 1ANS:IM5 3 4 5 6 6 81 2 22 0ans:39 3 13 4 5 6 to 6 8 9 1ans:49 2 3 4 5 6 6 8 9 1ans:im*/


CSU 1574 Amanda Lounges Simulation problem

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.