提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、题目
二、代码
代码如下(示例):
#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();
}
}