构造一个n个点,m条有向边的图,需要满足两个要求:
1.任意一对点对之间最多有一条有向边,且没有自环。
2.保证图联通,m条边的边权严格属于[1, m]且互不相同(1....M的一个排列),从任意点出发,经过任意路径后回到起始点,经过的边权总和是3的倍数。
其中第二个要求似乎听上去很玄乎,其实可以一步一步的来:要同时满足1.2的要求,而且输入数据 m >= n + 3,那么也就是说,我们总是能轻松的先构造一个n个点n条有向边的环,其中前n-1条边的边权是1...2..3...n-1,对于最后一条边,可以取n, n+1或 n+2,总之使得这个环的总权值tot%3==0就行了。
这样的话,我们就完成了初步建图:已经构造了一个n个点n条边且满足题意的图了,那么对于剩下的m-n条边怎么办?如果我们需要在原有的环上添加一条边权为w的边,并且还要维护图的1.2性质,显然,我们只需要找到这样一个点对<u, v>,其中在我们构造的初始环上,u - > v 的距离为g[u][v], 那么只需要g[u][v] % 3 == w % 3,我们就能保证图的性质不变了!
Assignment For Princess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 558 Accepted Submission(s): 174
Special Judge
Problem Description
Long long ago, in the Kingdom Far Far Away, there lived many little animals. And you are the beloved princess who is marrying the prince of a rich neighboring kingdom. The prince, who turns out to be a handsome guy, offered you a golden engagement ring that can run computer programs!
The wedding will be held next summer because your father, the king, wants you to finish your university first.
But you did’t even have a clue on your graduation project. Your terrible project was to construct a map for your kingdom. Your mother, the queen, wanted to make sure that you could graduate in time.
Or your wedding would have to be delayed to the next winter. So she told you how your ancestors built the kingdom which is called the Roads Principle:
1. Your kingdom consists of N castles and M directed roads.
2. There is at most one road between a pair of castles.
3. There won’t be any roads that start at one castle and lead to the same one.
She hoped those may be helpful to your project. Then you asked your cousin Coach Pang (Yes, he is your troubling cousin, he always asks you to solve all kinds of problems even you are a princess.), the Minister of Traffic, about the castles and roads. Your cousin, sadly, doesn’t have a map of the kingdom. Though he said the technology isn’t well developed and it depends on your generation to contribute to the map, he told you the Travelers Guide, the way travelers describe the amazing road system:
1. No matter which castle you start with, you can arrive at any other castles.
2. Traveling on theM roads will take 1, 2, 3, ... ,M days respectively, no two roads need the same number of days.
3. You can take a round trip starting at any castle, visiting a sequence of castles, perhaps visiting some castles or traveling on some roads more than once, and finish your journey where you started.
4. The total amount of days spent on any round trip will be a multiple of three.
But after a month, you still couldn’t make any progress. So your brother, the future king, asked your university to assign you a simpler project. And here comes the new requirements. Construct a map that satisfies both the Roads Principle and the Travelers Guide when N and M is given.
There would probably be several solutions, but your project would be accepted as long as it meets the two requirements.
Now the task is much easier, furthermore your fiance sent two assistants to help you.
Perhaps they could finish it within 5 hours and you can think of your sweet wedding now.
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, there is one line containing two integers N, M described above.(10 <= N <= 80, N+3 <= M <= N
2/7 )
Output
For each test case, first output a line “Case #x:”, where x is the case number (starting from 1).
Then output M lines for each test case. Each line contains three integers A, B, C separated by single space, which denotes a road from castle A to castle B and the road takes C days traveling.
Oh, one more thing about your project, remember to tell your mighty assistants that if they are certain that no map meets the requirements, print one line containing one integer -1 instead.
Note that you should not print any trailing spaces.
Sample Input
Sample Output
Case #1:
1 2 1
2 3 2
2 4 3
3 4 4
4 5 5
5 6 7
5 1 6
6 1 8
Hint
The restrictions like N >= 10 will be too big for a sample. So the sample is just a simple case for the detailed formats of input and output, and it may be helpful for a better understanding. Anyway it won’t appear in actual test cases.
Source
2013 Asia Chengdu Regional Contest
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int g[111][111];
int n,m;
int d[222][222];
const int inf = 0x3f3f3f3f;
int main()
{
int re ; cin>>re;
int ca = 1;
while(re--) {
cin>>n>>m;
memset(g,0,sizeof g);
for(int i=1;i<n;i++) g[i][i+1] = i;
int s = n*(n-1)/2;
int last = n;
if((last+s)%3==0) g[n][1]=last;
else {
last = n + 1;
if((last+s)%3==0) g[n][1]=last;
else {
last = n+2;
g[n][1]=last;
}
}
memset(d,0,sizeof d);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) {
if(i==j) continue;
int t=0;
for(int k=i;k!=j;)
{
int next = k + 1;
if(k==n) next = 1;
t+=g[k][next];
k=next;
}
d[i][j] = t;
}
printf("Case #%d:\n", ca++);
bool ok = 1;
for(int i=n;i<=m;i++)
{
if(i==last) continue;
bool f = 1;
for(int j=1;f && j<=n;j++)
for(int k=1;f && k<=n;k++) {
if(j==k||g[j][k]||g[k][j]) continue;
if(d[j][k]%3 == i%3)
{
g[j][k] = i, f = 0;
break;
}
}
if(f) { ok=0; break; }
}
if(!ok) { puts("-1"); continue; }
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(g[i][j])
printf("%d %d %d\n", i,j,g[i][j]);
}
}
}