【拓扑排序】 UVALive 6393 Self-Assembly

对于每一个方块,4个点建12条边,比如4个点是A+,B+,就建边A+ ---> B-然后拓扑判断是不是有环。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 600005
#define eps 1e-12
#define mod 998244353
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#pragma comment(linker, "/STACK:102400000,102400000")
#define pii pair<int, int>
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct Edge
{
	int v;
	Edge *next;
}*H[maxn], E[maxm], *edges;

int n, m;
queue<int> q;
char s[maxn];
int in[maxn];
int vis[maxn];

void addedges(int u, int v)
{
	edges->v = v;
	edges->next = H[u];
	H[u] = edges++;
}

void init()
{
	edges = E;
	memset(H, 0, sizeof H);
	memset(vis, 0, sizeof vis);
	memset(in, 0, sizeof in);
}

void work()
{
	for(int i = 1; i <= n; i++) {
		scanf("%s", s);
		for(int j = 0; j < 8; j += 2) {
			if(s[j] == '0') continue;
			for(int k = 0; k < 8; k += 2) {
				if(s[k] == '0') continue;
				if(j == k) continue;
				int u = s[j] - 'A';
				if(s[j+1] == '+') u += 26;
				int v = s[k] - 'A';
				if(s[k+1] == '-') v += 26;
				addedges(u, v);
				in[v]++;
			}
		}
	}
	int cnt = 0;
	for(int i = 0; i < 52; i++) if(!in[i]) cnt++, q.push(i);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(Edge *e = H[u]; e; e = e->next) {
			int v = e->v;
			in[v]--;
			if(!in[v]) cnt++, q.push(v);
		}
	}
	if(cnt == 52) printf("bounded\n");
	else printf("unbounded\n");
}

int main()
{
	while(scanf("%d", &n)!=EOF) {
		init();
		work();
	}
	
	
	return 0;
}


你可能感兴趣的:(拓扑排序)