【bzoj1093】 [ZJOI2007]最大半连通子图

*题目描述:
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意
两点u,v,存在一条u到v的有向路径或者从v到u的有向路径。若G’=(V’,E’)满足V’?V,E’是E中所有跟V’有关的边,
则称G’是G的一个导出子图。若G’是G的导出子图,且G’半连通,则称G’为G的半连通子图。若G’是G所有半连通子图
中包含节点数最多的,则称G’是G的最大半连通子图。给定一个有向图G,请求出G的最大半连通子图拥有的节点数K
,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。
*输入:
第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整
数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤1
00000, M ≤1000000;对于100%的数据, X ≤10^8
*输出:
应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.
*样例输入:
6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4
*样例输出:
3
3
*题解:
由题意可得,每一个强连通分量都是一个半连通子图,所以我们就先把所有的强连通分量缩起来,原图就变成了一幅DAG。然后我们在DAG上跑一边以size为关键词的最长链就好了(用拓扑排序)。
*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

#ifdef WIN32
    #define LL "%I64d"
#else
    #define LL "%lld"
#endif

#ifdef CT
    #define debug(...) printf(__VA_ARGS__)
    #define setfile() 
#else
    #define debug(...)
    #define filename ""
    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
    R char ch; R int cnt = 0; R bool minus = 0;
    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    ch == '-' ? minus = 1 : cnt = ch - '0';
    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    return minus ? -cnt : cnt;
}
#define maxn 100010
#define maxm 2000010
struct Edge
{
    int to;
    Edge *next;
}*last[maxn], e[maxm], *ecnt = e, *last2[maxn];
inline void link(R int a, R int b)
{
    *++ecnt = (Edge) {b, last[a]}; last[a] = ecnt;
}
inline void link2(R int a, R int b)
{
    *++ecnt = (Edge) {b, last2[a]}; last2[a] = ecnt;
}
int dfn[maxn], low[maxn], num, st[maxn], stcnt, id[maxn], scc, size[maxn];
void dfs(R int x)
{
    dfn[x] = low[x] = ++num;
    st[++stcnt] = x;
    for (R Edge *iter = last[x]; iter; iter = iter -> next)
    {
        R int pre = iter -> to;
        if (!dfn[pre])
        {
            dfs(pre);
            cmin(low[x], low[pre]);
        }
        else if (!id[pre]) cmin(low[x], dfn[pre]);
    }
    if (dfn[x] == low[x])
    {
        ++scc;
        for ( ; ; )
        {
            R int pre = st[stcnt--];
            id[pre] = scc;
            size[scc]++;
            if (pre == x) break;
        }
    }
}
int lastvis[maxn], deg[maxn], f1[maxn], f2[maxn];
std::queue<int> q;
int main()
{
//  setfile();
    R int n = FastIn(), m = FastIn(), mod = FastIn();
    for (R int i = 1; i <= m; ++i)
        link(FastIn(), FastIn());
    for (R int i = 1; i <= n; ++i)
        if (!dfn[i]) dfs(i);
    R int ans1 = 0, ans2;
    for (R int i = 1; i <= n; ++i)
        for (R Edge *iter = last[i]; iter; iter = iter -> next)
        {
            if (id[i] != id[iter -> to])
            {
                link2(id[i], id[iter -> to]);
                ++deg[id[iter -> to]];
            }
        }
    for (R int i = 1; i <= scc; ++i)
        if (!deg[i])
        {
            q.push(i);
            f1[i] = size[i];
            f2[i] = 1;
        }
    while (!q.empty())
    {
        R int now = q.front(); q.pop();
        for (R Edge *iter = last2[now]; iter; iter = iter -> next)
        {
            R int pre = iter -> to;
            --deg[pre];
            if (!deg[pre]) q.push(pre);
            if (lastvis[pre] == now) continue;
            lastvis[pre] = now;
            if (f1[pre] < f1[now] + size[pre])
            {
                f1[pre] = f1[now] + size[pre];
                f2[pre] = f2[now];
            }
            else if (f1[pre] == f1[now] + size[pre])
                f2[pre] = (f2[pre] + f2[now]) % mod;
        }
        if (ans1 < f1[now])
        {
            ans1 = f1[now];
            ans2 = f2[now];
        }
        else if (ans1 == f1[now]) ans2 = (ans2 + f2[now]) % mod;
    }
    printf("%d\n%d\n", ans1, ans2 );
    return 0;
}
posted @ 2016-05-16 16:52  cot  阅读(230)  评论(0编辑  收藏  举报