怎么会逝呢,调了一下午了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+4;
const int R=1e6+4;
const int A=N+2*R;
const int T=8;
ll n,r,c,t,ans,dis[A],dp[A];
ll cnt,len,dfn[A],low[A];
ll top,s[A],aft[A],num[A];
ll tot1,tot2,head1[N],head2[N];
ll dx[T]={1,1,-1,-1,1,-1,0,0};
ll dy[T]={1,-1,1,-1,0,0,1,-1};
bool ins[A];
queue<ll> q;
struct node1{
ll x,y,t;
}d[N];
struct node2{
ll a,b;
}e[A<<1];
struct Edge1{
ll to,nxt;
}e1[A<<1];
struct Edge2{
ll to,nxt;
}e2[A<<1];
void add_edge1(ll u,ll v){
e[++t].a=u;
e[t].b=v;
e1[++tot1].to=v;
e1[tot1].nxt=head1[u];
head1[u]=tot1;
}
void add_edge2(ll u,ll v){
e2[++tot2].to=v;
e2[tot2].nxt=head2[u];
head2[u]=tot2;
}
bool cmp(node1 a,node1 b){
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
ll work(ll tx,ll ty){
ll l=1,r=n;
while(l<=r){
ll mid=(l+r)>>1;
if(d[mid].x==tx&&d[mid].y==ty){
return mid;
}
if(d[mid].x<tx||d[mid].x==tx&&d[mid].y<ty){
l=mid+1;
}else{
r=mid-1;
}
}
return 0.677; //防伪认证
}
void Tarjan(ll u){
dfn[u]=low[u]=++cnt;
ins[u]=true;
s[++top]=u;
for(int i=head1[u];i;i=e1[i].nxt){
ll v=e1[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else{
if(ins[v]){
low[u]=min(low[u],dfn[v]);
}
}
}
if(dfn[u]==low[u]){
len++;
while(s[top+1]!=u){
aft[s[top]]=len;
ins[s[top]]=false;
num[len]+=(s[top]<=n);
top--;
}
}
}
void topo(){
for(int i=1;i<=len;i++){
if(!dis[i]){
q.push(i);
dp[i]=num[i];
}
}
while(!q.empty()){
ll u=q.front();
q.pop();
for(int i=head2[u];i;i=e2[i].nxt){
ll v=e2[i].to;
dp[v]=max(dp[v],dp[u]+num[v]);
if(!(--dis[v])){
q.push(v);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>r>>c;
for(int i=1;i<=n;i++){
cin>>d[i].x>>d[i].y>>d[i].t;
}
sort(d+1,d+n+1,cmp);
for(int i=1;i<=n;i++){
add_edge1(n+d[i].x,i);
add_edge1(n+r+d[i].y,i);
if(d[i].t==1){
add_edge1(i,n+d[i].x);
}
if(d[i].t==2){
add_edge1(i,n+r+d[i].y);
}
if(d[i].t==3){
for(int j=0;j<=7;j++){
ll tx=d[i].x+dx[j];
ll ty=d[i].y+dy[j];
if(tx<1||ty<1||tx>r||ty>c){
continue;
}
ll id=work(tx,ty);
if(!id){
continue;
}
add_edge1(i,id);
}
}
}
for(int i=1;i<=n+r+c;i++){
if(!dfn[i]){
Tarjan(i);
}
}
for(int i=1;i<=t;i++){
if(aft[e[i].a]!=aft[e[i].b]){
add_edge2(aft[e[i].a],aft[e[i].b]);
dis[aft[e[i].b]]++;
}
}
topo();
for(int i=1;i<=len;i++){
ans=max(ans,dp[i]);
}
cout<<ans;
}