CSU 1574 Amanda Lounges 模拟题

题目链接:点击打开链接

题意:

给定n个点m条边的无向图(开始每个点都是白色)

下面m行给出边和边权,边权表示这条边所连接的2个点中被染成黑色的点数。

0表示染,1表示其中一个点染,2表示都染。

问:最少染多少个点可以满足上述的边权。若不存在输出impossible

首先处理所有边权为0和2的情况。

这样处理后图中就只剩下边权为1的子图。

任意染一个点,然后bfs一下把子图染掉即可。

#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=getchar(),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];
//注意 N M 要修改
int head[N], edgenum;
void add(int u, int v, int col){
	Edge E = {u, v, col, head[u]};
	edge[edgenum] = E;
	head[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 && 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 4
1 2 2
2 3 1
3 4 1
4 5 1

ans:3

5 5
1 2 2
2 3 1
3 4 1
4 5 1
3 5 1

ans:im

5 6
1 2 2
2 3 1
3 4 1
4 5 1
3 6 0
5 6 0

ans:3

9 8
1 2 2
2 3 1
3 4 1
4 5 1
3 6 0
5 6 0
7 8 1
8 9 1

ans:4

9 9
1 2 2
2 3 1
3 4 1
4 5 1
3 6 0
5 6 0
7 8 1
8 9 1
9 7 1

ans:im
*/
时间: 2025-01-10 04:00:54

CSU 1574 Amanda Lounges 模拟题的相关文章

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

cf428c 模拟题

这题说的是给了 n个数然后又 k次 的交换任意位置的 数字的机会  计算最长的连续子序列的和 这要撸  模拟整个 过程 并不能就是算最长的递增序列 如果只是 找最长的 和序列的 话 会存在 很多问题 在替换的时候 每一个决策 都影响着 下一个决策  这样 存在谁与谁替换 这样的状态有 200!种    那就枚举每个区间这样就可以使得 我们所用替换方法得当  因为在替换中我们进行替换是对不同区间的 操作 比如 在替换序列之内的 数字的时候 其实操作的就是不同的区间 与外面的序列进行替换的时候 操作

TOJ1290 Poker Hands 模拟题

寒假期间抽空做的一道模拟题 难度不算大,把每种牌型分开处理,可以合并的步骤考虑合并. 代码比较丑陋,首次尝试Sport Programming的风格,结果搞了个不伦不类(手动笑哭) 1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 2414 Chessboard Dance(模拟题,仅此纪念我的堕落)

题目 模拟题也各种wa,我最近真的堕落了,,,,,智商越来越为负数了!!!!!!!! #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char mp[10][10]; int d=-1;//0shang,1xia,2zuo,3you int x,y;//weizhi int weizhi(int i,int j) { if(mp[i][j]=='<'){x=

HDU 4930 Fighting the Landlords(扯淡模拟题)

Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q (Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5 > 4 > 3. 给你8种组合:1.

锦程网考试由试题从模拟题中批量找出答案,Python

jincin的考试又来了,像往常一样会先有模拟题发下来,而考试题目几乎都在里面原题. 本来是,在考试时,把题目一题一题地在模拟题里搜,但觉得太累了. 于是写了一个Python的脚本批量查找,用到正则,由于不知道行尾是\r还是\n还是\r\n,干脆也用正则,而非split('\r')这么硬板了. 添了颜色,效果不错. Python: 效果: - 锦程网考试由试题从模拟题中批量找出答案,Python,布布扣,bubuko.com

Codeforces 48C The Race 模拟题

题目链接:点击打开链接 题意: 给定n个加油站,一辆车由A点跑到B点,每个100m有一个加油站,每开100m需要10升油. 在每个车站会检查一下油量,若车子若开不到下一个加油站则加x升油. 开始有x升油 下面给出加油的记录. 问下一次加油在哪一站.若答案唯一输出具体哪站. 油箱容量无限 思路: 水模拟.. #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

FZU Problem 2034 Password table (简单模拟题)

这种简单题做了好长时间,我是不是有点逗? 地址:http://acm.fzu.edu.cn/problem.php?pid=2034 不解释了,自己看吧,练手的好题 上个代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> #include <string.h> #include <stdlib.h>