luogu 2002 消息扩散
tarjan缩点
:对原图进行tarjan缩点,对缩后的点连边,统计入度为0的点。
luogu 1119 灾后重建
Floyed
:对于每个新加入的点,将其作为中转点,跑floyed。
luogu 1186 玛丽卡
最短路
:类似次小生成树,求出最短路径后,将其路径上的每一条边删去,再求一遍。
luogu 1462 通往奥格瑞玛的道路
最短路+二分
:二分最大值,将值大于二分值的都删去,看s和t是否连通。
luogu 1613 跑路
floyed+倍增
:首先类似传递闭包或倍增求出两点间是否存在一条(2^k)的道路,若存在初始化dis[i][j] = 1,然后跑floyed。
luogu 2700 逐个击破
最大生成树
:将边从大至小排序,将每个点看作一个集合,先将k个特殊点染色,然后从大至小枚举边,将边两边的点集合分情况合并:
- 一端染色,一端未染色:未染色端染上色,两集合合并。
- 两端都未染色: 两集合直接合并。
- 两端都染色: 加上该边的权值,合并集合。
code
luogu 2002
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50, M = 5e5 + 50;
int n, m, low[N], dfn[N], clk, sccno[N], sccnum[N], sccCnt, ans, outdeg[N], indeg[N], from[N];
vector<int> G[N];
stack<int> S;
inline void dfs(int u){
dfn[u] = low[u] = ++clk;
S.push(u);
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(!dfn[v]){
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]){
int x; sccCnt++;
for(;;){
x = S.top(); S.pop();
sccno[x] = sccCnt;
sccnum[sccCnt]++;
if(x == u) return;
}
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i<= m; i++){
int x, y; scanf("%d%d", &x, &y);
if(x != y) G[x].push_back(y);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) dfs(i);
// for(int i = 1; i <= n; i++) cout<<sccno[i]<<" "<<endl;
for(int i = 1; i <= n; i++){
for(int e = G[i].size() - 1; e >= 0; e--){
int v = G[i][e];
if(sccno[i] != sccno[v])
outdeg[sccno[i]]++, indeg[sccno[v]]++;
}
}
for(int i = 1; i <= sccCnt; i++){
if(!indeg[i] && !outdeg[i]) ans++;
else if(!indeg[i]) ans ++;
}
printf("%d", ans);
return 0;
}
luogu 1119
#include<bits/stdc++.h>
using namespace std;
const int N = 220, OO = 0x3f3f3f3f;
int n, m;
int f[N][N], q;
vector<int> h[1000000];
bool vst[N];
int main(){
scanf("%d%d", &n, &m);
for(int i = 1 ; i <= n; i++){
int x; scanf("%d", &x);
h[x].push_back(i);
}
memset(f, OO, sizeof f);
for(int i = 1; i <= n; i++) f[i][i] = 0;
for(int i = 1; i <= m; i++){
int u, v, w; scanf("%d%d%d", &u, &v, &w);
u++, v++;
f[u][v] = f[v][u] = min(f[u][v], w);
}
scanf("%d", &q);
int now = -1;
for(int i = 1; i <= q; i++){
int x, y, t; scanf("%d%d%d", &x, &y, &t);
x++, y++;
while(now < t){
now++;
for(int j = 0; j < h[now].size(); j++){
int u = h[now][j];
// cout<<now<<" "<<u<<endl;
vst[u] = true;
for(int k = 1; k <= n; k++)
for(int l = 1; l <= n; l++)
f[k][l] = min(f[k][l], f[k][u] + f[u][l]);
}
}
printf("%d
", (f[x][y] < OO && vst[x] && vst[y]) ? f[x][y] : -1);
}
return 0;
}
luogu 1186
#include<bits/stdc++.h>
using namespace std;
const int N = 1005, M = 1e6 + 5, OO = 0x3f3f3f3f;
int n, ans, tmp, m;
vector<pair<int, int> > G[N];
vector<pair<int, int> > route;
pair<int, int> from[N];
int dis[N];
#define mp make_pair
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
inline void dj(int s, int t, int f, int a, int b){
while(!que.empty()) que.pop();
memset(dis, OO, sizeof dis);
dis[s] = 0;
que.push(mp(0, s));
while(!que.empty()){
pair<int, int> tt = que.top(); que.pop();
int u = tt.second, dist = tt.first;
// cout<<u<<"->";
if(u == t) break;
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e].first;
int len = G[u][e].second;
if(f == 1 && ((u == a && v == b)|| (u == b && v == a))) continue;
// cout<<v<<" !!";
if(dis[v] > dis[u] + len){
from[v] = mp(u, len);
dis[v] = dis[u] + len;
que.push(mp(dis[v], v));
}
}
// cout<<endl;
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
int a, b, c; scanf("%d%d%d", &a ,&b, &c);
G[a].push_back(mp(b, c));
G[b].push_back(mp(a, c));
}
dj(1, n, 0, 0, 0);
ans = tmp = dis[n];
// cout<<dis[n]<<endl;
int now = n;
route.push_back(mp(now, 0));
while(from[now].first) route.push_back(from[now]), now = from[now].first;
// for(int i = 0; i <= route.size() - 1; i++) cout<<route[i].first<<" "<<route[i].second<<endl;
for(int i = 0; i < route.size() - 1; i++){
int u = route[i].first, v = route[i + 1].first;
dj(1, n, 1, u, v);
// cout<<u<<" "<<v<<" "<<dis[v]<<endl;
ans = max(ans, dis[n]);
}
printf("%d", ans);
}
luogu 1462
#include<bits/stdc++.h>
using namespace std;
const int N = 10005, M = 1e6 + 5;
const long long OO = 2e18;
int n, m, b;
long long maxx = -OO, minn = OO, f[N];
vector<pair<int, long long> > G[N];
long long dis[N];
#define mp make_pair
priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > que;
inline bool check(long long mid){
if(f[1] > mid) return false;
while(!que.empty()) que.pop();
for(int i = 0; i <= n; i++) dis[i] = OO;
dis[1] = 0;
que.push(mp(0, 1));
while(!que.empty()){
pair<long long, int> t = que.top(); que.pop();
if(t.second == n) return t.first <= b;
int u = t.second;
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e].first, len = G[u][e].second;
if(f[v] > mid) continue;
if(dis[v] > dis[u] + len){
dis[v] = dis[u] + len;
que.push(mp(dis[v], v));
}
}
}
return false;
}
int main(){
scanf("%d%d%d", &n, &m, &b);
for(int i = 1; i <= n; i++) scanf("%d", &f[i]), maxx = max(maxx, f[i]), minn = min(minn, f[i]);
for(int i = 1; i <= m; i++){
int x, y, c; scanf("%d%d%d", &x, &y, &c);
G[x].push_back(mp(y, c)), G[y].push_back(mp(x, c));
}
long long l = minn, r = maxx, ans = -1;
while(l <= r){
long long mid = l + r >> 1;
if(check(mid)) r = mid - 1, ans = mid;
else l = mid + 1;
}
if(ans == -1) printf("AFK");
else printf("%lld", ans);
return 0;
}
luogu1613
#include<bits/stdc++.h>
using namespace std;
const int N = 55, OO = 0x3f3f3f3f;
int n, dis[N][N], m;
bool f[N][N][35];
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
int x, y; scanf("%d%d", &x, &y);
f[x][y][0] = true;
}
memset(dis, OO, sizeof dis);
for(int l = 0; l <= 31; l++)
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
if(l) f[i][j][l]= f[i][j][l] || (f[i][k][l - 1] && f[k][j][l - 1]);
if(f[i][j][l]) dis[i][j] = 1;
}
// for(int i = 1; i <= n; i++)
// for(int j = 1; j <= n; j++)
// for(int l = 1; l <= 5; l++)
// cout<<i<<" "<<j<<" "<<l<<" "<<f[i][j][l]<<endl;
// for(int i = 1; i <= n; i++)
// for(int j = 1; j <= n; j++)
// cout<<i<<" "<<j<<" "<<dis[i][j]<<endl;
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
printf("%d", dis[1][n]);
return 0;
}
luogu2700
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+50;
int n, k, anc[N];
long long ans;
bool mark[N];
struct node{
int x, y, c;
inline bool operator < (const node &b) const{
return c > b.c;
}
}edge[N];
inline int getAnc(int x){
return x == anc[x] ? x : (anc[x] = getAnc(anc[x]));
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) anc[i] = i;
for(int i = 1; i <= k; i++){
int x; scanf("%d", &x);
x++;
mark[x] = true;
}
for(int i = 1; i < n; i++){
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
x++, y++;
edge[i] = (node){x, y, c};
}
sort(edge + 1, edge + n);
for(int i = 1; i < n; i++){
int x = edge[i].x, y = edge[i].y, c = edge[i].c;
int fx = getAnc(x), fy = getAnc(y);
if(fx != fy){
if(mark[fx] && !mark[fy]){
mark[fy] = true;
anc[fx] = fy;
}
else if(!mark[fx] && mark[fy])
anc[fx] = fy;
else if(mark[fx] && mark[fy]){
anc[fx] = fy;
ans += 1ll*c;
}
else if(!mark[fx] && !mark[fy])
anc[fx] = fy;
}
// cout<<x<<" "<<y<<" "<<c<<" "<<ans<<endl;
}
printf("%lld", ans);
return 0;
}