poj 1094 Sorting It All Out(java + 拓扑排序)
来源:互联网 发布:广州seo顾问 编辑:程序博客网 时间:2024/06/14 16:28
特殊测试数据
2 2
A<B
B<A
要输出
Sorted sequence determined after 1 relations: AB.
package 拓扑排序;
import java.util.Scanner;
/**问题请参考
* @author rayli
* @date:2014-7-30 上午9:51:58
* @version :
*
*/
public class SortingAllout
{
static int n;
static int m;
static int map[][];
static int re[];
int d[] = new int[n];
int topo(String s, int n)
{
char tmp[] = s.toCharArray();
int du[] = new int[n];
int one = tmp[0] - 'A';
int two = tmp[2] - 'A';
if(map[one][two] == 0)
{
map[one][two] = 1;
d[two]++;
}
for(int i=0; i<n; i++)
du[i] = d[i];
int w = 0;
int cnt;
int repos = 0;
boolean flag = true;
for(int i=0; i<n; i++)
{
cnt = 0;
for(int j=0; j<n; j++)
{
if(du[j] == 0)
{
w = j;
cnt ++;
}
}
if(cnt == 0)
return -1;
else if(cnt > 1)
flag = false;//之前写成true
du[w]--;
re[repos++] = w;
for(int j =0; j<n; j++)
{
if(map[w][j] == 1)
{
du[j]--;
}
}
}
if(flag)
return 1;
else
return 0;
}
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
m = cin.nextInt();
while(n!=0 && m!=0)
{
map = new int[n][n];
re = new int[n];
if(n -1 > m)
{
for(int i=0; i<m; i++)
{
cin.next();
}
System.out.println("Sorted sequence cannot be determined.");
}
else
{
SortingAllout sa = new SortingAllout();
//sa.ans(n, m);
String s;
boolean flag = true;
for(int i=0; i<m; i++)
{
s = cin.next();
if(flag)
{
int t = sa.topo(s, n);
if(t == 1)
{
System.out.print("Sorted sequence determined after " + (i+1) + " relations: ");
for(int j=0; j<n; j++)
{
System.out.print((char)(re[j] + 'A'));
}
System.out.println(".");
flag = false;
}
else if(t == -1)
{
System.out.println("Inconsistency found after " + (i+1) + " relations.");
flag = false;
}
}
}
if(flag)
System.out.println("Sorted sequence cannot be determined.");
}
n = cin.nextInt();
m = cin.nextInt();
}
cin.close();
}
}
- poj 1094 Sorting It All Out(java + 拓扑排序)
- POJ 1094 Sorting It All Out(拓扑排序)
- Poj 1094-Sorting It All Out [拓扑排序]
- POJ 1094-Sorting It All Out 拓扑排序
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out(拓扑排序)
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- Poj 1094 Sorting It All Out——拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序+Floyd算法
- poj1860 Currency Exchange(java + bellman-Ford)
- poj2485 Highways(prim java)
- poj 3026 Borg Maze(java bfs + prim)
- poj 1258 Agri-Net( java prim)
- poj 1789 Truck History(java + prim)
- poj 1094 Sorting It All Out(java + 拓扑排序)
- poj 3020 Antenna Placement(java + 匈牙利算法)
- Java项目经验——程序员成长的钥匙
- poj 3041 Asteroids(java + 匈牙利算法)
- int main(int argc,char* argv[])详
- matlab 记一次debug的心路历程以及NaN问题的解决办法
- 关于物联网的一点畅想
- restrict关键字
- 常有关pdf、360浏览器、Chrome浏览器和win7保护眼睛的颜色设置方法总结