題目
給定 n 個變量和 m 個不等式。其中 n 小于等于26,變量分别用前 n 的大寫英文字母表示。
不等式之間具有傳遞性,即若 A>B 且 B>C ,則 A>C。
請從前往後周遊每對關系,每次周遊時判斷:
如果能夠确定全部關系且無沖突,則結束循環,輸出确定的次序;
如果發生沖突,則結束循環,輸出有沖突;
如果循環結束時沒有發生上述兩種情況,則輸出無定解。
輸入格式
輸入包含多組測試資料。
每組測試資料,第一行包含兩個整數n和m。
接下來m行,每行包含一個不等式,不等式全部為小于關系。
當輸入一行0 0時,表示輸入終止。
輸出格式
每組資料輸出一個占一行的結果。
結果可能為下列三種之一:
如果可以确定兩兩之間的關系,則輸出 “Sorted sequence determined after t relations: yyy…y.”,其中’t’指疊代次數,'yyy…y’是指升序排列的所有變量。
如果有沖突,則輸出: “Inconsistency found after t relations.”,其中’t’指疊代次數。
如果沒有沖突,且不能确定兩兩之間的關系,則輸出 “Sorted sequence cannot be determined.”。
資料範圍
2≤n≤26,變量隻可能為大寫字母A~Z。
輸入樣例1:
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
輸出樣例1:
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
輸入樣例2:
6 6
A<F
B<D
C<E
F<D
D<E
E<F
0 0
輸出樣例2:
Inconsistency found after 6 relations.
輸入樣例3:
5 5
A<B
B<C
C<D
D<E
E<A
0 0
輸出樣例3:
Sorted sequence determined after 4 relations: ABCDE.
難度:簡單
時/空限制:1s / 10MB
總通過數:1104
總嘗試數:2616
來源:《算法競賽進階指南》
算法标簽
思路
- 假設d[i][j]=1表示i<j,=0表示i,j沒有關系,那麼判斷一個序列是否能唯一确定排序,隻要看一下是不是任意兩點的關系都确定了,也就是對于d[i][j]和d[j][i]隻能并且必須有一個為1,是以這裡可以用Floyd來求每一次加一條邊進去的情況,最後如果存在自環也就是d[i][i]=1則有沖突,如果不是任意兩點滿足d[i][j]和d[j][i]隻能并且必須有一個為1的條件,就是不能确定,如果都确定了就是能唯一排序了
- 能唯一排序後每一次取出全部沒有被排序的數中最小的值,一個數字如果最小那麼它肯定對于全部沒有被排序的數來說,沒有一個可以到達的,即d[i][j]為0,這樣就可以求出來最小值
- 複雜度 O(mn^3)
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 26;
int n, m;
bool g[N][N], d[N][N];
bool st[N];
void floyd()
{
memcpy(d, g, sizeof d);
for (int k = 0; k < n; k ++ )
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
d[i][j] |= d[i][k] && d[k][j];
}
int check()
{
for (int i = 0; i < n; i ++ )
if (d[i][i])
return 2;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < i; j ++ )
if (!d[i][j] && !d[j][i])
return 0;
return 1;
}
char get_min()
{
for (int i = 0; i < n; i ++ )//枚舉一下所有的點
if (!st[i])
{
bool flag = true;
for (int j = 0; j < n; j ++ )//枚舉對于該點i來說,看一下是否有點d[j][i]=1,即存在j點有i<j
if (!st[j] && d[j][i])
{
flag = false;
break;
}
if (flag)
{
st[i] = true;
return 'A' + i;
}
}
}
int main()
{
while (cin >> n >> m, n || m)
{
memset(g, 0, sizeof g);
int type = 0, t;
for (int i = 1; i <= m; i ++ )
{
char str[5];
cin >> str;
int a = str[0] - 'A', b = str[2] - 'A';
if (!type)
{
g[a][b] = 1;
floyd();
type = check();
if (type) t = i;
}
}
if (!type) puts("Sorted sequence cannot be determined.");
else if (type == 2) printf("Inconsistency found after %d relations.\n", t);
else
{
memset(st, 0, sizeof st);
printf("Sorted sequence determined after %d relations: ", t);
for (int i = 0; i < n; i ++ ) printf("%c", get_min());
printf(".\n");
}
}
return 0;
}
增量優化
- 由于每一次加邊不用都用一次Floyd,因為沒加入一條i到j的邊,可以帶來的所有更新關系隻有一下幾種
- ①:a可以到i,則d[a][j]=1
- ②:j可以到b,則d[i][b]=1
- ③:a可以到i,b可以到j,則d[a][b]=1
- 每一次加入着三種邊即可
- 複雜度O(m*n^2)
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 26;
int n, m;
bool d[N][N];
bool st[N];
int check()
{
for (int i = 0; i < n; i ++ )
if (d[i][i])
return 2;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < i; j ++ )
if (!d[i][j] && !d[j][i])
return 0;
return 1;
}
char get_min()
{
for (int i = 0; i < n; i ++ )
if (!st[i])
{
bool flag = true;
for (int j = 0; j < n; j ++ )
if (!st[j] && d[j][i])
{
flag = false;
break;
}
if (flag)
{
st[i] = true;
return 'A' + i;
}
}
}
int main()
{
while (cin >> n >> m, n || m)
{
memset(d, 0, sizeof d);
int type = 0, t;
for (int i = 1; i <= m; i ++ )
{
char str[5];
cin >> str;
int a = str[0] - 'A', b = str[2] - 'A';
if (!type)
{
d[a][b] = 1;
for (int x = 0; x < n; x ++ )
{
if (d[x][a]) d[x][b] = 1;
if (d[b][x]) d[a][x] = 1;
for (int y = 0; y < n; y ++ )
if (d[x][a] && d[b][y])
d[x][y] = 1;
}
type = check();
if (type) t = i;
}
}
if (!type) puts("Sorted sequence cannot be determined.");
else if (type == 2) printf("Inconsistency found after %d relations.\n", t);
else
{
memset(st, 0, sizeof st);
printf("Sorted sequence determined after %d relations: ", t);
for (int i = 0; i < n; i ++ ) printf("%c", get_min());
printf(".\n");
}
}
return 0;
}