ural 1080. Map Coloring

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1080


题意描述:尝试用两种颜色给地图染色,若成功则输出01字符串,默认从0开始,若失败则输出-1;


思路大致是边BFS边判断,用字符串的位存储01信息,之后直接输出字符串;


AC代码:

//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <stack>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <iomanip>
using namespace std;

vector<vector<int> >v;

void bfs(){
	string s(v.size(), '2');
	s[1] = '0';

	bool flag = false;
	queue<int>q;
	q.push(1);
	while (!q.empty()){
		int src = q.front(); q.pop();
		for (int tar : v[src]){
			if (s[tar] == '2'){
				s[tar] = s[src] == '0' ? '1' : '0';
				q.push(tar);
			}
			else if (s[tar] == s[src]){
				flag = true;
				break;
			}
		}
		if (flag)break;
	}

	if (flag)cout << -1 << endl;
	else cout << s.substr(1) << endl;
}

void func(){
	int n, tar;
	cin >> n;
	v.resize(n + 1, vector<int>());

	for (int i = 1; i <= n; i++){
		while (cin >> tar&&tar){
			v[i].push_back(tar);
			v[tar].push_back(i);
		}
	}
	bfs();
}

int main(){

	//freopen("out.txt", "w", stdout);
	//freopen("in.txt", "r", stdin);

	func();

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值