const int MAXV = 15;
struct DSU
{
int father[MAXV];
void init()
{
REP(i, MAXV) father[i] = i;
}
int find(int n)
{
return father[n] == n ? n : father[n] = find(father[n]);
}
void merge(int a, int b)
{
int fa = find(a), fb = find(b);
father[fa] = fb;
}
} dsu;
struct Graph
{
typedef pair<int, int> pii;
queue<int> q;
vector<int> vt[MAXV];
int in[MAXV], sum[MAXV];
bool vis[MAXV][MAXV];
void init()
{
REP(i, MAXV)
vt[i].clear();
while (!q.empty())
q.pop();
CLR(in, 0);
CLR(sum, 0);
CLR(vis, false);
}
void add(int a, int b)
{
if (vis[a][b]) return;
vt[a].push_back(b);
vis[a][b] = true;
in[b]++;
}
void solve(int n)
{
CLR(vis, 0);
FE(i, 0, n)
if (in[i] == 0)
q.push(i);
int k = 0;
while (!q.empty())
{
int t = q.front();
q.pop();
int len = vt[t].size();
sum[t] = k++;
REP(i, len)
{
int next = vt[t][i];
if (!vis[t][next])
{
vis[t][next] = true;
if (--in[next] == 0)
q.push(next);
}
}
}
FE(i, 0, n)
sum[i] = sum[dsu.find(i)];
printf("%d", sum[1] - sum[0]);
FE(i, 2, n)
printf(" %d", sum[i] - sum[i - 1]);
puts(" ");
}
} graph;
char ipt[200];
int main()
{
// freopen("in.txt", "r", stdin);
int T, n;
RI(T);
FE(kase, 1, T)
{
vector<int> vt;
dsu.init();
graph.init();
RI(n);
RS(ipt);
int cnt = 0;
FE(i, 1, n) FE(j, i, n)
{
if (ipt[cnt] == ‘0‘)
dsu.merge(i - 1, j);
cnt++;
}
cnt = 0;
FE(i, 1, n) FE(j, i, n)
{
if (ipt[cnt] == ‘-‘)
graph.add(dsu.find(j), dsu.find(i - 1));
else if (ipt[cnt] == ‘+‘)
graph.add(dsu.find(i - 1), dsu.find(j));
cnt++;
}
graph.solve(n);
}
return 0;
}原文:http://blog.csdn.net/wty__/article/details/21975275