lzm_7.28-tarjan专题测
T1
思路
只需要判断图中有没有环即可,这里我写了
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int n, m, ans, in[N];
vector<int> v[N];
queue<int> q;
signed main(){
//freopen("T1.in", "r", stdin);
//freopen("T1.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1, x, y; i <= m; i++){
cin >> x >> y;
v[x].push_back(y);
in[y]++;
}
for(int i = 1; i <= n; i++){
if(!in[i]){
q.push(i);
}
}
while(q.size()){
int p = q.front();
q.pop();
for(int i : v[p]){
if(!--in[i]){
q.push(i);
}
}
}
int f = 0;
for(int i = 1; i <= n; i++){
if(in[i]){
f = 1;
}
}
if(f) cout << "Yes";
else cout << "No";
return 0;
}
T3
思路
要求每个点可以到达的其它点的个数,注意到
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, ans[N], dfn[N], low[N], tot, scc_cnt, p[N], cnt[N], x[N], y[N], op[N], in[N], dp[N];
vector<int> v[N], s, v2[N];
void tarjan(int x){
dfn[x] = low[x] = ++tot;
op[x] = 1;
s.push_back(x);
for(int i : v[x]){
if(!dfn[i]){
tarjan(i);
low[x] = min(low[x], low[i]);
}else if(op[i]){
low[x] = min(low[x], dfn[i]);
}
}
if(dfn[x] == low[x]){
scc_cnt++;
while(s.size()){
int tmp = s.back();
s.pop_back();
op[tmp] = 0;
p[tmp] = scc_cnt;
cnt[scc_cnt]++;
if(tmp == x){
break;
}
}
}
}
void topo(){
queue<int> q;
for(int i = 1; i <= scc_cnt; i++){
dp[i] = cnt[i];
if(!in[i]){
q.push(i);
}
}
while(q.size()){
int p = q.front();
q.pop();
for(int i : v2[p]){
dp[i] += dp[p];
if(!--in[i]){
q.push(i);
}
}
}
}
signed main(){
//freopen("P12760_46.in", "r", stdin);
//freopen("T3.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> x[i] >> y[i];
v[x[i]].push_back(y[i]);
}
for(int i = 1; i <= n; i++){
if(!dfn[i]){
tarjan(i);
}
}
for(int i = 1; i <= m; i++){
if(p[x[i]] != p[y[i]]){
v2[p[y[i]]].push_back(p[x[i]]);
in[p[x[i]]]++;
}
}
topo();
for(int i = 1; i <= n; i++){
cout << dp[p[i]] - 1 << '\n';
}
return 0;
}
T4
思路
首先
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m, k, l, cnt, tot, cnt1, root, dfn[N], low[N], ans[N], to[N], head[N], nxt[N], siza[N], sizb[N];
struct edge{
int u, v;
}a[N];
void add(int u, int v){
nxt[++cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
}
void Add(int u, int v){
a[++cnt1] = {u, v};
}
void tarjan(int u, int fa){
dfn[u] = low[u] = ++tot;
for (int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if (!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
siza[u] += siza[v], sizb[u] += sizb[v];
if (low[v] > dfn[u] && (!siza[v] || !sizb[v] || siza[v] == k || sizb[v] == l)) {
Add(u, v);
}
}else if (v != fa){
low[u] = min(low[u], dfn[v]);
}
}
}
signed main(){
cin >> n >> m >> k >> l;
fill(head + 1, head + 1 + n, -1);
for (int i = 1; i <= k; i++){
int x;
cin >> x;
siza[x]++;
}
for (int i = 1; i <= l; i++){
int x;
cin >> x;
sizb[x]++;
}
for (int i = 1, u, v; i <= m; i++){
cin >> u >> v;
add(u, v);
add(v, u);
}
for (int i = 1; i <= n; i++){
if (!dfn[i]){
tarjan(i, 0);
}
}
cout << cnt1 << '\n';
for (int i = 1; i <= cnt1; i++)cout << a[i].u << ' ' << a[i].v << '\n';
return 0;
}
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, ans, dfn[N], low[N], tot, scc_cnt, p[N], cnt[N], x[N], y[N], op[N], in[N][2], dp[N][2];
vector<int> v[N], s, v2[N][2];
void tarjan(int x){
dfn[x] = low[x] = ++tot;
op[x] = 1;
s.push_back(x);
for(int i : v[x]){
if(!dfn[i]){
tarjan(i);
low[x] = min(low[x], low[i]);
}else if(op[i]){
low[x] = min(low[x], dfn[i]);
}
}
if(dfn[x] == low[x]){
scc_cnt++;
while(s.size()){
int tmp = s.back();
s.pop_back();
op[tmp] = 0;
p[tmp] = scc_cnt;
cnt[scc_cnt]++;
if(tmp == x){
break;
}
}
}
}
void topo(int k){
queue<int> q;
dp[p[1]][k] = cnt[p[1]];
for(int i = 1; i <= scc_cnt; i++){
if(!in[i][k]){
q.push(i);
}
}
while(q.size()){
int p = q.front();
q.pop();
for(int i : v2[p][k]){
dp[i][k] = max(dp[i][k], dp[p][k] + cnt[i]);
if(!--in[i][k]){
q.push(i);
}
}
}
}
signed main(){
//freopen("P12760_46.in", "r", stdin);
//freopen("T3.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> x[i] >> y[i];
v[x[i]].push_back(y[i]);
}
for(int i = 1; i <= n; i++){
if(!dfn[i]){
tarjan(i);
}
}
ans = cnt[p[1]];
for(int i = 1; i <= m; i++){
if(p[x[i]] != p[y[i]]){
v2[p[x[i]]][0].push_back(p[y[i]]);
v2[p[y[i]]][1].push_back(p[x[i]]);
in[p[y[i]]][0]++;
in[p[x[i]]][1]++;
}
}
memset(dp, -0x3f, sizeof(dp));
topo(0);
topo(1);
for(int i = 1; i <= m; i++){
if(p[x[i]] != p[y[i]]){
ans = max(dp[p[y[i]]][0] + dp[p[x[i]]][1] - cnt[p[1]], ans);
}
}
cout << ans;
return 0;
}