P8456 「SWTR-8」地地铁铁
题意
给定一张
分析
路径问题考虑缩点,因为简单路径的定义是不经过重复点的路径,所以选择点双缩点。
发现满足条件的点对数量直接做有点难做,考虑求不满足条件的点对数量,即,所有
Case 1: 之间的路径都是全 或全
全
点双基本性质:给定点
点双进阶性质:给定点 不会严谨证明 感性理解即可。
由此有结论:若点双
考虑建出圆方树,若一个点双
最后需要求一条边所属的点双。还是在圆方树上考虑问题,发现一定是两端点中深度最深的那个点的父亲方点。
Case 2: 之间的路径既有全 又有全
不难发现若满足条件则全
结论:一个点双里至多有一对合法点对。感性理解,若存在另一组合法点对,则可以构造出一条合法点对之间同时经过 0 权和 1 权的路径。
现在只需要判断是否存在这样的点对即可。
还有结论:若恰好存在两个点满足该点在点双内同时有 0 权和 1 权的出边,则存在这样的点对。必要性显然,充分性感性理解。
时间复杂度
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=4e5+5,maxm=1e6+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
i64 ans;
struct edge{
int u,v,w;
}e[maxm];
vector<pii>G[maxn];
vector<int>E[2];
void add(int x,int y,int z){
G[x].emplace_back(mp(y,z)),G[y].emplace_back(mp(x,z));
}
int dfn[maxn],low[maxn],dfncnt;
int sta[maxn],tp;
int sc;
vector<int>v[maxn];
int bel[maxm];
void tar(int x,int y){
dfn[x]=low[x]=++dfncnt,sta[++tp]=x;
for(pii _:G[x]){
int u=_.fi;
if(!dfn[u]){
tar(u,x);
ckmn(low[x],low[u]);
if(low[u]>=dfn[x]){
++sc;int nw;
do{nw=sta[tp],v[sc].emplace_back(nw),--tp;}while(nw^u);
v[sc].emplace_back(x);
}
}else if(u^y){
ckmn(low[x],dfn[u]);
}
}
}
vector<int>H[maxn<<1];
bool vis[maxn<<1];
void add_tree(int x,int y){
H[x].emplace_back(y),H[y].emplace_back(x);
}
i64 calc(int x){
return 1ll*x*(x-1)/2;
}
int dep[maxn<<1],fa[maxn<<1];
void dfs0(int x,int y){
fa[x]=y,dep[x]=dep[y]+1;
for(int u:H[x])if(u^y)dfs0(u,x);
}
int siz[maxn<<1];
void dfs(int x,int y){
siz[x]=(x<=n);
for(int u:H[x])if(u^y){
dfs(u,x);
if(vis[x])ans-=calc(siz[u]);
else siz[x]+=siz[u];
}
}
vector<int>be[maxn];
int S[maxn];
inline void solve_the_problem(){
n=rd(),m=rd();
rep(i,1,m){
int x,y;char z;
x=rd(),y=rd(),z=gc();
int t=z=='D';
e[i]=(edge){x,y,t};
add(x,y,i),E[t].emplace_back(i);
}
// PU;
// rep(i,1,m)write(e[i].u,32),write(e[i].v,32),write(e[i].w);
ans=calc(n);
tar(1,0);
rep(i,1,sc)for(int u:v[i])add_tree(u,i+n);
dfs0(1,0);
rep(i,1,m){
int x=e[i].u,y=e[i].v;
if(dep[x]<dep[y])swap(x,y);
bel[i]=fa[x]-n,be[bel[i]].emplace_back(i);
}
// write(sc);
// rep(i,1,sc){
// for(int u:v[i])write(u,32);P__;
// }
// rep(i,1,m)write(bel[i],32);P__;
rep(c,0,1){
rep(i,1,n+sc)vis[i]=0,siz[i]=0;
for(int i:E[c^1])vis[bel[i]+n]=1;
dfs(1,0);
ans-=calc(siz[1]);
}
rep(i,1,sc){
for(int u:v[i])S[u]=0;
int sum=0;
for(int u:be[i]){
int x=e[u].u,y=e[u].v,z=e[u].w;
S[x]|=(1<<z),S[y]|=(1<<z);
}
for(int u:v[i])if(S[u]==3)++sum;
if(sum==2)--ans;
}
write(ans);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int ID=rd(),_=1;
while(_--)solve_the_problem(),--ID;
}
/*
0
4 3
1 2 d
2 3 D
3 4 d
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现