代码如下:
#include<bits/stdc++.h>
using i8=char; using u8=unsigned char;
using i16=short; using u16=unsigned short;
using i32=int; using u32=unsigned int;
using i64=long long;using u64=unsigned long long;
using i128=__int128;using u128=unsigned __int128;
using f32=float; using f64=double; using f128=long double;
template<typename T> inline void read(T &x){
char ch=getchar(),f=0;
x=0;
while(ch<'0'||ch>'9')f|=(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9')(x=x*10+(ch-'0')),ch=getchar();
x=(f?-x:x);
}
template<typename T,typename ...Ts> void read(T &x,Ts &...xs){read(x); read(xs...); }
char fostk[50]; u8 cntfostk;
template<typename T> inline void write(T x){
if(x<0)putchar('-'),x=-x;
while(x>9)fostk[++cntfostk]='0'+x%10,x/=10;
fostk[++cntfostk]=x+'0';
while(cntfostk)putchar(fostk[cntfostk--]);
}
template<typename T> void writesp(T x){write(x); putchar(' '); }
template<typename T> void writeln(T x){write(x); putchar('\n'); }
template<typename T> void writelns(T x){writeln(x); }
template<typename T,typename ...Ts> void write(T x,Ts ...xs){writesp(x); write(xs...); }
template<typename T,typename ...Ts> void writeln(T x,Ts ...xs){write(x,xs...); putchar('\n'); }
template<typename T,typename ...Ts> void writelns(T x,Ts ...xs){writeln(x); writelns(xs...); }
constexpr i32 dx[]={-1,-1,-1,0,0,1,1,1}, dy[]={-1,0,1,-1,1,-1,0,1};
i32 n,r,c;
struct Node{
i32 bel,dfn,low;
bool ins,tag;
static i32 cntdfn;
}nod[12000050];
struct Point{
i32 x,y,t;
}pt[10000050];
struct Scc{
i32 size,vis,dp;
static i32 cntscc;
}scc[12000050];
i32 Node::cntdfn=0,Scc::cntscc=0;
i32 rp(i32 x){return n+x; }
i32 cp(i32 x){return n+r+x; }
std::basic_string<i32> G[1200005],DAG[1200005];
std::map<std::tuple<i32,i32>,i32> mp;
std::stack<i32> s;
void tarjan(i32 x){
nod[x].dfn=++Node::cntdfn;
nod[x].low=nod[x].dfn;
nod[x].ins=true;
s.push(x);
for(auto v:G[x]){
if(!nod[v].dfn){
tarjan(v);
nod[x].low=std::min(nod[x].low,nod[v].low);
}else if(nod[v].ins)nod[x].low=std::min(nod[x].low,nod[v].low);
}
if(nod[x].low==nod[x].dfn){
i32 now=++Scc::cntscc,y;
do{
y=s.top();
s.pop();
scc[now].size+=nod[y].tag;
nod[y].bel=now;
nod[y].ins=false;
}while(x!=y);
}
}
i32 dfs(i32 x){
if(scc[x].vis)return scc[x].dp;
scc[x].vis=true;
i32 re=scc[x].size,big=0;;
for(auto v:DAG[x]){
big=std::max(big,dfs(v));
}
return scc[x].dp=re+big;
}
i32 main(){
#ifdef LOCAL
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
freopen("test.err","w",stderr);
#endif
read(n,r,c);
for(i32 i=1;i<=n;i++){
read(pt[i].x,pt[i].y,pt[i].t);
nod[i].tag=1;
mp[std::make_tuple(pt[i].x,pt[i].y)]=i;
}
for(i32 i=1;i<=n;i++){
G[rp(pt[i].x)]+=i;
G[cp(pt[i].y)]+=i;
if(pt[i].t==1){
G[i]+=rp(pt[i].x);
}else if(pt[i].t==2){
G[i]+=cp(pt[i].y);
}else{
for(i32 j=0;j<8;j++){
auto tmp=std::make_tuple(pt[i].x+dx[j],pt[i].y+dy[j]);
if(mp.find(tmp)!=mp.end()){
G[i]+=mp[tmp];
// printf("tmp.x:%d tmp.y:%d\n",)
}
}
}
}
for(i32 i=1;i<=n+r+c;i++){
if(!nod[i].dfn)tarjan(i);
}
for(i32 i=1;i<=n+r+c;i++){
for(auto v:G[i]){
if(nod[v].bel==nod[i].bel)continue;
DAG[nod[i].bel]+=nod[v].bel;
}
}
// for(i32 i=1;i<=Scc::cntscc;i++){
// for(auto v:DAG[i]){
// // printf("i:%d scc[i].size:%d v:%d scc[i].size:%d\n",i,scc[i].size,v,scc[v].size);
// }
// }
i32 ans=0;
// for(i32 i=1;i<=n+r+c;i++){
// for(auto v:G[i]){
// printf("i:%d v:%d bel[i]:%d bel[v]:%d\n",i,v,nod[i].bel,nod[v].bel);
// }
// }
// printf("Scc::cntscc:%d\n",Scc::cntscc);
for(i32 i=1;i<=Scc::cntscc;i++){
// printf("scc[i:%d].size:%d\n",i,scc[i].size);
if(!scc[i].vis)ans=std::max(ans,dfs(i));
}
write(ans);
return 0;
}
帮帮蒟蒻吧