# hihocoder #1185 : 连通性·三 tarjan

### #1185 : 连通性·三

#### 描述

1->2->4 		total: 11
1->3->5 		total: 9
1->3->6->3->5: 		total: 13


#### 输出

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define fir first
#define se second
#define pb(x) push_back(x)
#define ALL(V) (V).begin(), (V).end()
const int maxn = 1e5 + 10;
const int mod = 1000000007;
const int oo = 0x3f3f3f3f;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef vector<int> vii;

int n, m, t, id;
int a[maxn];
int na[maxn];
stack<int> s;
vii g[maxn], rg[maxn];
vii ng[maxn];
vii ans[maxn];
int chk[maxn];
int grp[maxn];

void init() {
for(int i = 0; i < maxn; i++) g[i].clear();
for(int i = 0; i < maxn; i++) rg[i].clear();
for(int i = 0; i < maxn; i++) ng[i].clear();
for(int i = 0; i < maxn; i++) ans[i].clear();
clr(a, 0); clr(na, 0);
clr(chk, 0); clr(grp, 0);
while(!s.empty()) s.pop();
id = 0;
}

void dfs(int x) {
if(chk[x] == t) return;
chk[x] = t;
for(int i : g[x]) dfs(i);
s.push(x);
}

void dfs2(int x) {
if(chk[x] == t) return;
chk[x] = t; ans[id].pb(x);
grp[x] = id;
for(int i : rg[x]) dfs2(i);
}

int fans;
int vis[maxn];
void dfs3(int x, int sum) {
fans = max(fans, sum);
for(int i : ng[x]) {
dfs3(i, sum + na[i]);
}
}

int main() {
//freopen("d:/in.txt", "r", stdin);
//freopen("d:/out2.txt", "w", stdout);
while(~scanf("%d %d", &n, &m)) {
init();
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].pb(v), rg[v].pb(u);
}
t = 1;
for(int i = 1; i <= n; i++) {
if(chk[i] != t) {
dfs(i);
}
}
t = 2;
for(int i = 1; i <= n; i++) {
int tmp = s.top();
if(chk[tmp] != t) {
++id;
dfs2(tmp);
}
s.pop();
}
for(int i = 1; i <= id; i++) {
int sum = 0;
for(int j : ans[i]) {
for(int k : g[j]) {
if(grp[k] != i)
ng[i].pb(grp[k]);
}
for(int k : rg[j]) {
if(grp[k] != i)
ng[grp[k]].pb(i);
}
sum += a[j];
}
na[i] = sum;
}
fans = 0;
clr(vis, 0);
//printf("check %d %d\n", grp[2], a[grp[2]]);
dfs3(grp[1], na[grp[1]]);
printf("%d\n", fans);
}
}

posted @ 2016-10-26 08:59  MartinEden  阅读(...)  评论(...编辑  收藏