# 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp

6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4

3
3

Tarjan+拓扑排序+dp

#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cstring>
#define N 100010
using namespace std;
queue<int> q;
vector<int> e[N] , v[N];
int p , deep[N] , low[N] , tot , ins[N] , sta[N] , top , bl[N] , si[N] , num , ind[N] , last[N];
struct data
{
int x , y;
data(int a = 0 , int b = 0) {x = a , y = b;}
data operator+(int a) {return data(x + a , y);}
data operator^(data a)
{
if(x > a.x) return *this;
else if(x < a.x) return a;
else return data(x , (y + a.y) % p);
}
}f[N];
void tarjan(int x)
{
vector<int>::iterator i;
deep[x] = low[x] = ++tot , ins[x] = 1 , sta[++top] = x;
for(i = e[x].begin() ; i != e[x].end() ; i ++ )
{
if(!deep[*i]) tarjan(*i) , low[x] = min(low[x] , low[*i]);
else if(ins[*i]) low[x] = min(low[x] , deep[*i]);
}
if(deep[x] == low[x])
{
int t;
num ++ ;
do
{
t = sta[top -- ] , ins[t] = 0;
bl[t] = num , si[num] ++ ;
}while(t != x);
}
}
void solve(int n)
{
vector<int>::iterator i;
data ans;
int x;
for(x = 1 ; x <= n ; x ++ )
for(i = e[x].begin() ; i != e[x].end() ; i ++ )
if(bl[x] != bl[*i])
v[bl[x]].push_back(bl[*i]) , ind[bl[*i]] ++ ;
for(x = 1 ; x <= num ; x ++ )
if(!ind[x])
f[x] = data(si[x] , 1) , q.push(x);
while(!q.empty())
{
x = q.front() , q.pop() , ans = ans ^ f[x];
for(i = v[x].begin() ; i != v[x].end() ; i ++ )
{
if(last[*i] != x) last[*i] = x , f[*i] = f[*i] ^ (f[x] + si[*i]);
ind[*i] -- ;
if(!ind[*i]) q.push(*i);
}
}
printf("%d\n%d\n" , ans.x , ans.y);
}
inline char nc()
{
static char buf[100000] , *p1 , *p2;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf , 1 , 100000 , stdin) , p1 == p2) ? EOF : *p1 ++ ;
}
{
int ret = 0; char ch = nc();
while(!isdigit(ch)) ch = nc();
while(isdigit(ch)) ret = ((ret + (ret << 2)) << 1) + (ch ^ '0') , ch = nc();
return ret;
}
int main()
{
int n = read() , m = read() , i , x , y;
for(i = 1 ; i <= m ; i ++ ) x = read() , y = read() , e[x].push_back(y);
for(i = 1 ; i <= n ; i ++ )
if(!deep[i])
tarjan(i);
solve(n);
return 0;
}


posted @ 2017-11-24 09:06  GXZlegend  阅读(...)  评论(...编辑  收藏