Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

temp file

Posted by whitesweater at 2008-10-12 16:47:43
#include <cstdio>
#include <cstring>
#define MAXV 100005
#define MAXE 500005

struct edge {
	int y, z;
	int next;
};

int n, m;
int first[MAXV], state[MAXV];
edge edges[MAXE * 2];
int que[MAXV], front, back;

int main() {
	int i, j, k, x, y, z, p1, p2;
	int ret = 0;

//	freopen( "b.in", "r", stdin );

	memset( first, -1, sizeof(first) );
	scanf( " %d %d", &n, &m );
	for( i = 0; i < m; i ++ ) {
		scanf( " %d %d %d %d", &x, &y, &p1, &p2 );
		x--, y--;
		edges[i].y = y;
		edges[i].z = (p1 == p2) ? -1 : 1;
		edges[i].next = first[x];
		first[x] = i;
		edges[i + m].y = x;
		edges[i + m].z = (p1 == p2) ? -1 : 1;
		edges[i + m].next = first[y];
		first[y] = i + m;
	}

	for( i = 0; i < n; i ++ )
		if( !state[i] ) {
			int cnt = 0;
			front = back = 0;
			state[i] = 1;
			que[back ++] = i;
			while( front < back ) {
				x = que[front ++];
				if( state[x] > 0 ) cnt ++;
				for( i = first[x]; i >= 0; i = edges[i].next ) {
					y = edges[i].y;
					z = state[x] * edges[i].z;
					if( !state[y] ) {
						state[y] = z;
						que[back ++] = y;
					} else {
						if( state[y] != z ) {
							ret = -1;
							goto finish;
						}
					}
				}
			}
			if( cnt > back-cnt ) {
				cnt = back-cnt;
				for( int it = 0; it < back; it ++ )
					state[que[it]] *= -1;
			}
			ret += cnt;
		}

finish: ;
	if( ret < 0 ) printf( "NO\n" );
	else {
		printf( "YES\n" );
		printf( "%d\n", ret );
		for( i = 0; i < n; i ++ )
			if( state[i] > 0 ) printf( "%d\n", i+1 );
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator