Train Splittin

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、题目

传送门

二、代码

代码如下(示例):

#include <iostream>
#include <cstring>
#include <algorithm>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<unordered_map>

using namespace std;
using ll = long long;
const int N = 1e5+10;
/*
对于任何公司来说,应该存在两个城市,以便仅使用该公司运营的路线无法从另一个城市到达一个城市。
另一方面,对于任何两家公司,应该可以仅使用这两家公司运营的路线从任何其他城市到达每个城市。
*/
void slove() {
	ll n, m;
	cin >> n >> m;
	vector<ll>a(N), b(N), c(N, 0);
	//a,b统计一条铁路的两端,c统计一个城市的铁路(出和入条数)
	for (int i = 1; i <= m; i++) {
		cin >> a[i] >> b[i];
		c[a[i]]++;
		c[b[i]]++;
	}
	if (m == n * (n - 1) / 2) {//表明在这一个图中任意两城市都可以互相到达(无向图所以/2)
		cout << "3\n";
		for (int i = 1; i <= m; i++) {
			if (a[i] + b[i] == 3) {//因为n>=3,让1-2这条铁路给公司1
				cout << "1 ";
			}
			else if (a[i] == 1 || b[i] == 1) {//把所有包含城市1的铁路给公司2
				cout << "2 ";
			}
			else {
				cout << "3 ";//其余的全部给公司3
			}
		}
	}
	else {
		ll cnt = -1;
		for (int i = 1; i <= n; i++) {
			if (c[i] != n - 1) {//找到一个城市它不能到其他任意城市(和一些城市之间没有道路,无法到达)
				cnt = i;//记录这一个城市,结束循环
				break;
			}
		}
		cout << "2\n";
		for (int i = 1; i <= m; i++) {
			if (a[i] == cnt || b[i] == cnt) {
				cout << "1 ";
			}
			else {
				cout << "2 ";
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	for (cin>>T; T--; cout << endl) {
		slove();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值