rt。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5,W=1e6+5;
int n,r,c,tot=0,ans=0,dp[N],cnt[N],a[N],belong[N],in[N],dfn[N],low[N],scc[N],dfscnt,scccnt;
bool vis[N],inst[N];
struct Node{int x,y,c;}p[N];
vector<int> h[W][2],l[W][2],G[N],newG[N];
map<pair<int,int>,int> hav;
void build_G()
{
for (int i=1;i<=r;i++)
{
if (h[i][0].size()>=1) tot++;
for (int x:h[i][0]) belong[x]=tot,cnt[tot]++;
}
for (int i=1;i<=c;i++)
{
if (l[i][0].size()>=1) tot++;
for (int x:l[i][0]) belong[x]=tot,cnt[tot]++;
}
for (int i=1;i<=n;i++)
if (belong[i]==0) belong[i]=++tot,cnt[tot]++;
for (int i=1;i<=r;i++)
{
for (int j=0;j<h[i][1].size();j++) h[i][1][j]=belong[h[i][1][j]];
sort(h[i][1].begin(),h[i][1].end());unique(h[i][1].begin(),h[i][1].end());
}
for (int i=1;i<=c;i++)
{
for (int j=0;j<l[i][1].size();j++) l[i][1][j]=belong[l[i][1][j]];
sort(l[i][1].begin(),l[i][1].end());unique(l[i][1].begin(),l[i][1].end());
}
for (int i=1;i<=n;i++)
{
if (vis[belong[i]]) continue;
if (p[i].c==1) {for (int x:h[p[i].x][1]) G[belong[i]].push_back(x);}
else if (p[i].c==2) {for (int x:l[p[i].y][1]) G[belong[i]].push_back(x);}
else
{
for (int x=p[i].x-1;x<=p[i].x+1;x++)
for (int y=p[i].y-1;y<=p[i].y+1;y++)
if (!(x==p[i].x&&y==p[i].y)&&hav[make_pair(x,y)]!=0) G[belong[i]].push_back(belong[hav[make_pair(x,y)]]);
}
vis[belong[i]]=1;
}
}
stack<int> st;
void Tarjan(int u)
{
low[u]=dfn[u]=++dfscnt;
inst[u]=1,st.push(u);
for (int v:G[u])
{
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (inst[v]) low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
scccnt++;
while (st.top()!=u) {scc[st.top()]=scccnt,inst[st.top()]=0,a[scccnt]+=cnt[st.top()];st.pop();}
st.pop();
scc[u]=scccnt,inst[u]=0,a[scccnt]+=cnt[u];
}
}
void build_newG()
{
for (int u=1;u<=n;u++)
for (int v:G[u])
if (scc[u]!=scc[v]) newG[scc[u]].push_back(scc[v]),in[scc[v]]++;
}
queue<int> q;
void Topo()
{
for (int i=1;i<=scccnt;i++)
if (!in[i]) q.push(i),dp[i]=a[i];
while (!q.empty())
{
int u=q.front();q.pop();
for (int v:newG[u])
{
dp[v]=max(dp[v],dp[u]+a[v]);in[v]--;
if (!in[v]) q.push(v);
}
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>r>>c;
for (int i=1;i<=n;i++)
{
cin>>p[i].x>>p[i].y>>p[i].c;
if (p[i].c==1) h[p[i].x][0].push_back(i),l[p[i].y][1].push_back(i);
else if (p[i].c==2) h[p[i].x][1].push_back(i),l[p[i].y][0].push_back(i);
else h[p[i].x][1].push_back(i),l[p[i].y][1].push_back(i);
hav[make_pair(p[i].x,p[i].y)]=i;
}
build_G();
for (int i=1;i<=tot;i++)
if (!dfn[i]) Tarjan(i);
build_newG();Topo();
for (int i=1;i<=scccnt;i++) ans=max(ans,dp[i]);
cout<<ans<<"\n";
return 0;
}