https://cn.vjudge.net/problem/Gym-101986F
总结:
非根节点u是割点,当且仅当其存在一个子节点v,使得v及其子节点都无法通过除了边(u,v) 外访问回其根节点的祖先,即 dfn[u]≤low[v]。
(u,v) 是桥当且仅当v及其子节点都无法通过除了边(u,v) 外访问回其根节点及其祖先,即 dfn[u]<low[v]。注意割点的定义,桥并不要求dfs树树根特判。
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
//#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=1e5+9;
struct E
{
int to;
ll w;
int nex;
}edge[maxn<<1],edge2[maxn<<1],edge3[maxn<<1];
int tot;
int head[maxn];
void addedge(int u,int v,ll w)
{
edge[tot].to=v;
edge[tot].w=w;
edge[tot].nex=head[u];
head[u]=tot++;
}
int tot2;
int head2[maxn];
void addedge2(int u,int v,ll w)
{
edge2[tot2].to=v;
edge2[tot2].w=w;
edge2[tot2].nex=head2[u];
head2[u]=tot2++;
}
int n,m;
struct node
{
int a,b,c;
int id;
}p[maxn];
ll d[maxn];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > >q;
void dj(int s)
{
mem(d,INF);
d[s]=0;
q.push({0,s});
while(!q.empty())
{
int u=q.top().second;
ll w=q.top().first;
q.pop();
//cout<<u<<endl;
for(int i=head[u];i!=-1;i=edge[i].nex)
{
int v=edge[i].to;
ll w=edge[i].w;
if(d[u]+w<d[v])
{
d[v]=d[u]+w;
q.push({d[v],v});
}
}
}
}
ll d2[maxn];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > >q2;
void dj2(int s)
{
mem(d2,INF);
d2[s]=0;
q2.push({0,s});
while(!q2.empty())
{
int u=q2.top().second;
ll w=q2.top().first;
q2.pop();
//cout<<u<<endl;
for(int i=head2[u];i!=-1;i=edge2[i].nex)
{
int v=edge2[i].to;
ll w=edge2[i].w;
if(d2[u]+w<d2[v])
{
d2[v]=d2[u]+w;
q2.push({d2[v],v});
}
}
}
}
int tot3;
int head3[maxn];
void addedge3(int u,int v,ll w)
{
edge3[tot3].to=v;
edge3[tot3].w=w;
edge3[tot3].nex=head3[u];
head3[u]=tot3++;
}
int dfn[maxn],low[maxn];
int cixu;
int father[maxn];
int ans[maxn];
void tarjan(int u,int fa)
{
int k=0;
dfn[u]=low[u]=++cixu;
father[u]=fa;
for(int i=head3[u];i!=-1;i=edge3[i].nex)
{
int v=edge3[i].to;
int id=edge3[i].w;
if(v==fa&&!k)//去重边
{
k++;
continue;
}
if(dfn[v]==0)
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) ans[id]=2;
}
else low[u]=min(low[u],dfn[v]);
}
}
void init()
{
tot=0;
tot2=0;
tot3=0;
mem(head2,-1);
mem(head,-1);
mem(head3,-1);
cixu=0;
mem(dfn,0);
mem(father,0);
mem(low,0);
}
int main()
{
//freopen("input.txt","r",stdin);
init();
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>p[i].a>>p[i].b>>p[i].c;
p[i].id=i;
addedge(p[i].a,p[i].b,p[i].c);
addedge2(p[i].b,p[i].a,p[i].c);
}
dj(1);
dj2(2);
//for(int i=1;i<=n;i++) cout<<d[i]<<" ";
//cout<<endl;
//for(int i=1;i<=n;i++) cout<<d2[i]<<" ";
//cout<<endl;
for(int i=1;i<=m;i++)
{
int u=p[i].a;
int v=p[i].b;
ll w=p[i].c;
int id=p[i].id;
if(d[u]+d2[v]+w==d[2])
{
addedge3(u,v,id);
addedge3(v,u,id);
}
else if(d[v]+d2[u]+w<d[2])
{
ans[id]=1;
}
}
tarjan(1,0);
for(int i=1;i<=m;i++)
{
if(ans[i]==1) puts("HAPPY");
else if(ans[i]==2) puts("SAD");
else puts("SOSO");
}
return 0;
}