LA 6393 Self-Assembly

题意:给你一些模块,如果接口字母相同而极性相反,那么就可以把两个模块连接起来。问你是否可以用当前种类的模块无限的连接。

方法:把每个模块看成边,举例,如果一个模块上有 X+ 和 Y- , 那么 就从X-引一条边到Y-, 从 Y+引一条边到X+。建图后检查有向图是不是acyclic的,如果有有cylce,那么就可以无限的连接,unbounded。否则只能有限的连接。


Code: 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>

#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline '\n'

#define test if(1)if(0)cerr
using namespace std;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

inline int id(const string &str)
{
    if (str[0] == '0') return -1;
    int result = str[0]-'A';
    result = (result<<1) + (str[1] == '+');
    return result;
}
const int maxn = 26*2;
vector<int> g[maxn];
int state[maxn];
int n;

bool dfs(int cur)
{
    state[cur] = 0;
    for (auto nxt : g[cur])
    {
        if (!state[nxt] || (state[nxt]==-1 && !dfs(nxt)))
            return false;
    }
    state[cur] = 1;
    return true;
}
bool toposort()
{
    Reset(state, -1);
    rep(i, maxn)    if (state[i] == -1 && !dfs(i))  return false;
    return true;
    
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (cin >> n)
    {
        rep(i, maxn)    g[i].clear();
        //input
        {
            string str;
            rep(i, n)
            {
                cin >> str;
                rep(i, 4)
                {
                    int id1 = id(str.substr(2*i,2));
                    if (id1 == -1)  continue;
                    for (int j = i+1; j < 4; ++j)
                    {
                        int id2 = id(str.substr(2*j,2));
                        if (id2 == -1)  continue;
                        g[id1^1].pb(id2);
                        g[id2^1].pb(id1);
                    }
                }
            }
            if (!toposort()) cout << "unbounded\n";
            else    cout << "bounded\n";
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值