一周的拓扑就先告一段落吧。
zstu 3999图论基础:邻接表(first)
http://blog.youkuaiyun.com/u011580493/article/details/24530305
简单的拓扑排序。老板给员工福利,某些员工要求:他比谁谁谁要多。福利最低为888,要尽量省钱。
根据拓扑排序,边的终点的那个人福利 + 1即可。
本题我是:例如a b,说明a 要比b的福利多。则a作为该边的终点,b作为该边的起点。
代码就不贴了,太挫。
并查集 + 拓扑排序。
uncertain:同时出现两个入度inDegree为0(若两个值相等的话,只算一个)的入口,则uncertain成立。
conflict:出现环的情况。
做法:
利用并查集,把相等的点用其中一个点来代替。
所有点的入度全部累加到该点,相邻的边也一并连接到该点的邻接表后面;
并且在遍历线性表时跳转到该点上来。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <queue>
#define debug cout<<"here"<<endl;
using namespace std;
const int SIZE = 10005;
int n,m;
int xb;
int head[SIZE],end[SIZE];
int bcj[SIZE],mark[SIZE],inDegree[SIZE];
int conflict, uncertain;
int FindRoot(int rt)
{
return bcj[rt] == rt?rt:bcj[rt] = FindRoot(bcj[rt]);
}
struct node
{
int v,next;
}a[SIZE<<1];
void push(int row,int num)
{
a[xb].v = num;
a[xb].next = head[row];
if(end[row] == -1)
{
end[row] = xb;
}
head[row] = xb++;
}
void init()
{
xb = conflict = uncertain = 0;
memset(head,-1,sizeof(head));
memset(end,-1,sizeof(end));
memset(mark,0,sizeof(mark));
memset(inDegree,0,sizeof(inDegree));
int t1,t2;
char ch;
for(int i = 0;i < n;i++)
{
bcj[i] = i;
}
for(int i = 0;i < m;i++)
{
scanf("%d %c %d",&t1,&ch,&t2);
if(ch == '=')
{
bcj[FindRoot(t2)] = FindRoot(t1);
}
else if(ch == '>')
{
push(t1,t2);
inDegree[t2] ++;
}
else
{
push(t2,t1);
inDegree[t1] ++;
}
}
}
int dealbcj()
{
for(int i = 0;i < n;i++)
{
FindRoot(i);
}
int tmp,cot = 0;
for(int i = 0;i < n;i++)
{
tmp = FindRoot(i);
if(i != tmp)
{
inDegree[tmp] += inDegree[i];
mark[i] = 1;//don't push into queue
//link
if(end[i] != -1)
{
a[end[i]].next = head[tmp];
head[tmp] = head[i];
}
}
else
{
cot++;
}
}
//cout<<"dealbcj"<<endl;
//cout<<"rp = "<<cot<<endl;
return cot;
}
void deal(int rp)
{
queue <int> que;
for(int i = 0;i < n;i++)
{
if(inDegree[i] == 0&&mark[i] == 0)
{
que.push(i);
mark[i] = 1;
}
}
int cot = 0;
while(!que.empty())
{
if(que.size() > 1)
{
//cout<<"que.size() = "<<que.size()<<endl;
uncertain = 1;
}
int f = que.front();
que.pop();
cot++;
int xb2= head[f],rnext;
while(xb2 != -1)
{
rnext = FindRoot(a[xb2].v);
inDegree[rnext] --;
if(inDegree[rnext] == 0 && mark[rnext] == 0)
{
mark[rnext] = 1;
que.push(rnext);
}
xb2 = a[xb2].next;
}
}
//cout<<"cot = "<<cot<<endl;
if(cot < rp)
conflict = 1;
if(conflict == 1)
{
printf("CONFLICT\n");
}
else if(uncertain == 1)
{
printf("UNCERTAIN\n");
}
else
{
printf("OK\n");
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
int rp = dealbcj();//real rest of people
deal(rp);
}
return 0;
}
HDU 3342
普通的拓扑排序,主要是判断是否有环。
已经写过了,直接戳下面这个地址
http://blog.youkuaiyun.com/u011580493/article/details/24424759
普通拓扑排序,题意一开始发现好难看懂,直接看样例。
输入一个n,后面跟着n行。
第i行,表示第i的儿子分别是多少,以0结束。
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
using namespace std;
const int SIZE = 105;
int map[SIZE][SIZE];
int mark[SIZE];
int inDegree[SIZE];
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
int tmp;
memset(map,0,sizeof(map));
memset(mark,0,sizeof(mark));
memset(inDegree,0,sizeof(inDegree));
//input
for(int i = 1;i <= n;i++)
{
while(scanf("%d",&tmp),tmp)
{
if(map[i][tmp] == 0)
{
map[i][tmp] = 1;
inDegree[tmp] ++;
}
}
}
queue<int>que;
for(int i = 1;i <= n;i++)
{
if(inDegree[i] == 0)
{
mark[i] = 1;
que.push(i);
}
}
int cot = 0;
while(!que.empty())
{
int top = que.front();
que.pop();
for(int i = 1;i <= n;i++)
{
if(map[top][i] == 1)
{
inDegree[i] --;
if(inDegree[i] == 0 && mark[i] == 0)
{
mark[i] = 1;
que.push(i);
}
}
}
printf("%d%c",top,cot++ == n-1?'\n':' ');
}
}
return 0;
}