# BZOJ1093: [ZJOI2007]最大半连通子图(tarjan dp)

## 题意

，以及不同的最大半连通子图的数目C。由于C可能比较大，仅要求输出C对X的余数。

## Sol

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
//#define int long long
using namespace std;
const int MAXN = 2 * 1e5 + 10;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, mod;
vector<int> v[MAXN], E[MAXN];
int low[MAXN], dfn[MAXN], col[MAXN], vis[MAXN], tot, cn, f[MAXN], g[MAXN], inder[MAXN], siz[MAXN], fuck[MAXN];
stack<int> s;
void tarjan(int x) {
low[x] = dfn[x] = ++tot;
vis[x] = 1; s.push(x);
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(!dfn[to]) tarjan(to), low[x] = min(low[x], low[to]);
else if(vis[to]) low[x] = min(low[x], dfn[to]);
}
if(dfn[x] == low[x]) {
int h; cn++;
do {
h = s.top(); s.pop();
col[h] = cn; vis[h] = 0;
siz[cn]++;
}while(x != h);
}
}
void Topsort() {
queue<int> q;
for(int i = 1; i <= cn; i++) {
if(!inder[i]) q.push(i);
f[i] = siz[i], g[i] = 1;
}
Pair ans = MP(0, 0);
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = 0; i < E[p].size(); i++) {
int to = E[p][i], val = f[p] + siz[to];
inder[to]--;
if(!inder[to]) q.push(to);
if(fuck[to] == p) continue;
if(f[to] == val) (g[to] += g[p]) %= mod;
else if(val > f[to]) f[to] = val, g[to] = g[p] % mod;
fuck[to] = p;
}
}
for(int i = 1; i <= cn; i++)
if(f[i] > ans.fi)
ans.fi = f[i];
for(int i = 1; i <= cn; i++)
if(f[i] == ans.fi)
ans.se = (ans.se + g[i]) % mod;
printf("%d\n%d", ans.fi, ans.se % mod);
}
main() {
//    freopen("3.in", "r", stdin);
for(int i = 1; i <= M; i++) {
v[x].push_back(y);
}
for(int i = 1; i <= N; i++)
if(!dfn[i]) tarjan(i);
for(int x = 1; x <= N; x++) {
//    sort(v[x].begin(), v[x].end(), comp);
//    int last = -1;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
//    if(i > 0 && (col[to] == col[last])) continue;
if(col[x] != col[to])
E[col[x]].push_back(col[to]), inder[col[to]]++;
//        last = to;
}
}
Topsort();
return 0;
}
/*
*/
posted @ 2018-09-01 10:40  自为风月马前卒  阅读(...)  评论(...编辑  收藏

……