Comunicating the Tibet(dfs染色)
题目链接:https://cn.vjudge.net/problem/1040907/origin
The Himalayas form the highest mountain range in the world (with mountains as high as 7,200 meters). Located in Asia, this mountain system has parts in the Bhutan, China, India, Nepal, and Pakistan. In China, the Tibet is a region delimited by the Himalayas.
The Tibet, which is the highest region on Earth, is an autonomous region inside the People's Republic of China. In this region there are Buddhist temples with Tibetan monks, which are visited every year by many people from the whole world. To facilitate communication, the Central Tibetan Administration (ACT, in Portuguese) wishes to install some radio-frequency antennas around the Tibet. The ACT considered the N most important temples where they wish to install these antennas. The World Communication Association (ACM, in Portuguese) was hired to setup and configure these antennas at the temples. ACM charged their star engineer Mashiro "thunderstorm" Sanae with this job.
After reading about the history and customs of Tibetan monks, Mashiro found a curious fact: the Tibetans have a particular fixation for the number K. By studying the map with the location of the N temples, Mashiro noticed that if he walks through distinct temples until he returns to the starting one, the number of temples he goes through (including the first) never has the form mK + 1 for any nonnegative integer m. He was fascinated with his discovery, and realized the importance of the number Kfor Tibetans. Mashiro knows that, when setting up the antennas, neighboring temples should receive distinct frequencies to avoid interference. Having this constraint in mind and knowing the importance of the number K for the Tibetans, Mashiro wishes to find an assignment of frequencies to antennas that uses at most K distinct frequencies, or determine that this assignment is impossible.
Mashiro had an idea to solve this problem and has already started coding it. Can you beat him to it?
Input
The first line has three integers, N, M, and K, the number of temples, the number of paths that join neighboring temples and the special number revered by the Tibetan people, respectively. The temples are represented by numbers from 1 to N.
The next M lines contain two distinct integers each. Each pair of integers represents two neighboring temples. No two lines among these M lines contain the same pair of integers.
- 1 ≤ N ≤ 5·104
- 0 ≤ M ≤ 5·105
- 1 ≤ K ≤ N
- 1 ≤ fi ≤ K
Output
If there is no possible frequency assignment to the temples, print a line containing "-1" (without the double quotes). Otherwise, print N lines. The i-th line must contain a single integer, fi, the frequency assigned to temple i, where 1 ≤ fi ≤ K. If there is more than one solution, any one will do.
Examples
Input
4 0 1
Output
1
1
1
1
Input
3 3 3
1 2
2 3
1 3
Output
1
2
3
Input
3 2 1
1 2
2 3
Output
-1
还是题意怎么也看不懂,直接看简化后的题意吧。
题意:无向图,n个点,m条边,现在有k种颜色,给每一个节点染色,要求相连的两点颜色是不同的。
思路:用dfs便利整个图,因为每个节点不一定都联通,所以用for循环来dfs每个点,当我们每赋值一个节点时,必须马上考虑该节点周围的值,这样才能保证得到最优策略,至于如何判断k种颜色是否够用,我们直接假设有2*种颜色,在每次取最优解得情况下如果还有节点的col[i]大于k,那么k种颜色一定是不够用的。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
vector <int> a[maxn]; // 存图
int col[maxn]; // 存颜色
int n,m,k;
void dfs(int v)
{
if ( col[v]!=0 ) {
return ;
}
int len = a[v].size();
for ( int i=1; i<=2*k; i++ ) { // 假设有2*k 种颜色
int j;
for ( j=0; j<len; j++ ) {
int u = a[v][j];
if ( col[u]==i ) { // 如果相邻点有i号颜色,不能进行染色
break ;
}
}
if ( j==len ) {
col[v] = i;
break ;
}
}
for ( int i=0; i<len; i++ ) { // bfs相邻点
int u = a[v][i];
dfs(u);
}
}
int main()
{
int i,j,x,y;
cin >> n >> m >> k;
for ( i=0; i<m; i++ ) {
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for ( i=1; i<=n; i++ ) {
if ( col[i]==0 ) { // 图不一定联通
dfs(i); // 用for循环,来bfs每个点
}
}
for ( int i=1; i<=n; i++ ) {
if ( col[i]>k ) { // 如果有个点的颜色值超过k,那么不可行
cout << "-1" << endl;
return 0;
}
}
for ( int i=1; i<=n; i++ ) {
cout << col[i] << endl;
}
return 0;
}