跳到正文

个人记录

NOTE 6.3

Amiyawasdonkey约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年6月3日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 13 分钟

版本

  1. v1当前

6.2在打模拟赛……rk13/38,95pts

今天上课啥都没听懂,对其他人进行了一个请教,现在回过头来再总结一下。

Tarjan

通过维护 dfn (表示该节点是第几个被dfs到的)以及 low (表示该节点所属强连通分量的最上面的节点)来求一个图内的强连通分量。

Code

C++
/*
1、low相等的点属于同一个强连通分量(low值正确)
2、dfn和low相等的点是它所在强连通分量的最上面的点
*/
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define pii pair<int, int>
#define all(d) (d).begin(), (d).end()
#define For(a, b, c) for(int a = b; a <= c; a++)
#define Ror(a, b, c) for(int a = b; a >= c; a--)
const int N = 1e5 + 5;
 
int n, m, t, cnt;
int dfn[N], low[N];
int bel[N]; // i属于的强连通分量
bool inst[N], vis[N];
stack<int> st;
vector<int> e[N], ans[N];

void dfs(int i) {
    t++; // 第几个被dfs的点
    dfn[i] = low[i] = t; // i最初的low是i本身;
    st.push(i); inst[i] = true;
    for(auto j : e[i]) {
        if(dfn[j] == 0) { // 树边
            dfs(j);
            low[i] = min(low[i], low[j]); 
        } else { // 回边 or 横插边
            if(inst[j]) 
                low[i] = min(low[i], dfn[j]);
        }
    }
    if(dfn[i] == low[i]) {
        cnt++; // 强连通分量的个数
        ans[cnt].push_back(i);
        while(st.top() != i) { // 栈中在i上的点与i在同一个强连通分量
            bel[st.top()] = cnt;
            inst[st.top()] = false;
            ans[cnt].push_back(st.top());
            st.pop();
        }
        st.pop();
        inst[i] = false;
        bel[i] = cnt;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    For(i, 1, m) {
        int u, v;
        cin >> u >> v;
        e[u].push_back(v);
    }
    For(i, 1, n) {
        if(!dfn[i]) dfs(i);
    }
    cout << cnt << '\n';
    For(i, 1, cnt) sort(ans[i].begin(),ans[i].end());
	For(i, 1, n) {
		if(vis[bel[i]]) continue;
		vis[bel[i]] = 1;
        For(j, 0, ans[bel[i]].size() - 1)
            cout << ans[bel[i]][j] << ' ';
        cout << '\n';
	}
    return 0;
}

Dijkstra

求单源最短路的算法之一,注意其不能处理带有负权值边的题目,带有负权值的边的题目应该使用SPFA
核心是松弛操作:当dfs到一个点的时候,如果儿子到起点的距离大于父亲到起点的距离+边的权值,则将儿子与起点的距离更新

Code

C++
#include<bits/stdc++.h>
#define Iseri namespace
#define Nina std
#define Kawaragi int
#define Momoka main
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
#define ll long long
#define ull unsigned long long
#define endl "\n"
const int maxn=100005;

using Iseri Nina;

inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

//===========================================================

struct node{//以到起点距离的排序方法存节点
    ll dis;
    ll p;
    friend bool operator < (node A, node B) {
		return A.dis > B.dis;
	}
};

ll n,m,s,u,v,w,d[maxn],vis[maxn];
vector<pair<ll,ll> >t[maxn];
priority_queue<node>q;//Dijkstra的堆优化

inline void dijkstra(){
	d[s]=0;
	q.push({0,s});//初始先把起点入队
	while(!q.empty()){
		ll x=q.top().p;
		q.pop();
		if(!vis[x]){
			vis[x]=1;
			for(auto i:t[x]){
				ll y=i.first;//儿子
				if(d[y]>d[x]+i.second/*该边的权值*/){//松弛操作
					d[y]=d[x]+i.second;
					q.push({d[y],y});
				}
			}
		}
	}
}
 
Kawaragi Momoka(){
	memset(d,0x7f7f7f7f,sizeof(d));
	n=read(),m=read(),s=read();
	for(ll i=1;i<=m;i++){
		u=read(),v=read(),w=read();
		t[u].emplace_back(v,w);
	}

	dijkstra();
	for(ll i=1;i<=n;i++)printf("%lld ",d[i]);
	return 0;
}

SPFA

求单源最短路的另一个算法,只能处理图中有负权值边的题目,不能处理所有边都是正权值的题目。

不同于Bellman-Ford每次遍历所有边,SPFA使用一个队列来维护那些距离更新后可能影响其他节点的顶点。只有当一个顶点的距离发生变化时,它才会被加入队列,从而减少了不必要的计算。

负权回路检测:可以通过记录每个节点被更新的次数,如果某个节点的更新次数超过顶点数,则说明图中存在负权回路。

Code

C++
int dis[N],inq[N],n,m,k,W;//dis[i]是到i的权值,inq为标记数组 
struct edg{
	int to,w;//去的地方,权值 
};
vector<edg>g[N];
void spfa(int u){
	for(int i = 0; i <= n; i++){
		dis[i]=1e9;//初始化 
		inq[i]=0;
	}
	dis[u]=0;
	inq[u]=1;
	queue<int> q;
	q.push(u);
	while(!q.empty()){
		u=q.front();
		inq[u]=0;//取消标记
		q.pop();
		for(int i = 0; i < g[u].size(); i++){
			int v=g[u][i].to;
			int w=g[u][i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;//更新(类似于Dijkstra算法) 
				if(!inq[v]){//没有被标记 
					inq[v]=1;
					q.push(v);
				}
			}
		}
	}
}

拓扑排序

在有向无环图中 保证排序之后每个节点的儿子一定在他父亲的后面出现

使用队列维护,当一个节点的入度是0时,将它入队,输出,并将这个节点删除,同时使其所有儿子的入度减一,并将它出队

Code

C++
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define For(a, b, c) for(int a = b; a <= c; a++)
#define Ror(a, b, c) for(int a = b; a >= c; a--)
const LL N = 1e4 + 5;

int n, rd[N];//rd存每个节点的入度
vector<int> e[N];
queue<int> q;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    For(i, 1, n) {
        int a;
        cin >> a;
        while(a != 0) {
            e[i].push_back(a);
            rd[a]++;
            cin >> a;
        }
    }
    For(i, 1, n) 
        if(rd[i] == 0) q.push(i);
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        cout << x << ' ';
        for(auto i : e[x]) {
            rd[i]--;
            if(rd[i] == 0) q.push(i);
        }
    }
    return 0;
}

2-SAT

2-SAT 问题的目标是给每个变量赋值使得所有条件得到满足。
板子

此类问题难点在于如何给每个节点的不同状态连边,具体题目具体分析。
之后用Tarjan找强连通分量,若一个节点的不同状态属于同一个强连通分量,那么无解/不成立,否则有解/成立。

Code

C++
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define pii pair<int, int>
#define For(a, b, c) for(int a = b; a <= c; a++)
#define Ror(a, b, c) for(int a = b; a >= c; a--)
const int N = 2e6 + 5;

int n, m;
int t, cnt;
int dfn[N], low[N], bel[N];
bool inst[N];
stack<int> st;
vector<int> e[N];

void dfs(int i) {
	t++;
	dfn[i] = low[i] = t;
	st.push(i); inst[i] = true;
	for(auto j : e[i]) {
		if(dfn[j] == 0) {
			dfs(j);
			low[i] = min(low[i], low[j]);
		} 
		else if(inst[j]) low[i] = min(low[i], dfn[j]);
	}
	if(dfn[i] == low[i]) {
		cnt++;
		while(st.top() != i) {
			bel[st.top()] = cnt;
			inst[st.top()] = false;
			st.pop();
		}
		bel[i] = cnt;
		inst[i] = false;
		st.pop();
	}
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    For(i, 1, m) {
    	int a, x, b, y;
    	cin >> a >> x >> b >> y;
    	if(x == 0 && y == 0) {
    		e[a + n].push_back(b);
    		e[b + n].push_back(a);
		} else if(x == 0 && y == 1) {
			e[a + n].push_back(b + n);
			e[b].push_back(a);
		} else if(x == 1 && y == 0) {
			e[a].push_back(b);
			e[b + n].push_back(a + n);
		} else {
			e[a].push_back(b + n);
			e[b].push_back(a + n);
		}
	}
	For(i, 1, 2*n)
		if(dfn[i] == 0) dfs(i);
	For(i, 1, n) {
		if(bel[i] == bel[i + n]) {
			cout << "IMPOSSIBLE";
			return 0;
		}
	}
	cout << "POSSIBLE\n";
	For(i, 1, n) {
		if(bel[i] > bel[i + n]) cout << 1 << ' ';
		else cout << 0 << ' ';
	}
    return 0;
}
原文