DSU on tree TLE求调
查看原帖
DSU on tree TLE求调
748694
yinianxingkong楼主2024/3/4 10:30

rt,时间复杂度应该没问题,求代码错误/卡常。

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#define int long long
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
int precision=3,POW[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};char ibuf[IOSIZE],obuf[IOSIZE],*p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
#endif
template<typename T>inline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))if(ch==45)w=-1;if(ch==EOF)return 0;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*w;}template<typename T>inline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))if(ch==45)w=-1;if(ch==EOF)return 0;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*=w,1;}inline bool read(char&s){while(s=getchar(),isspace(s));return 1;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return 0;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return 1;}template<typename T>inline void print(T x){if(x<0)putchar(45),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}inline bool read(std::string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return 0;while(!isspace(ch))s+=ch,ch=getchar();return 1;}inline void print(std::string x){for(int i=0,n=x.size();i<n;i++)putchar(x[i]);}inline bool read(bool&b){char ch;while(ch=getchar(),isspace(ch));b=ch^48;return 1;}inline void print(bool b){putchar(b+48);}inline bool read(double&x){int a=0,b=0;char ch=getchar();bool f=0;while(ch<48||ch>57){if(ch==45)f=1;ch=getchar();}while(47<ch&&ch<58){a=(a<<1)+(a<<3)+(ch^48);ch=getchar();}if(ch!=46){x=f?-a:a;return 1;}ch=getchar();while(47<ch&&ch<58){b=(b<<1)+(b<<3)+(ch^48),ch=getchar();}x=b;while(x>1)x/=10;x=f?-a-x:a+x;return 1;}inline void print(double x){if(x<0){putchar(45),x=-x;}x=round((long double)x*POW[precision])/POW[precision],print((long long)x),putchar(46),x-=(long long)(x);for(int i=1;i<=precision;i++)x*=10,putchar(x+48),x-=(int)x;}template<typename T,typename...T1>inline int read(T& a,T1&...other){return read(a)+read(other...);}template<typename T,typename...T1>inline void print(T a,T1...other){print(a),print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;template<typename T>Fast_IO& operator>>(Fast_IO&io,T&b){return read(b),io;}template<typename T>Fast_IO& operator<<(Fast_IO&io,T b){return print(b),io;}
#define cout io
#define cin io
#define endl '\n'
}using namespace fast_IO;
namespace TYX_YNXK{
	#define il inline
	#define bl bool
	#define ll long long
	#define ull unsigned int
	#define vd void
	#define N 300005
	#define INF 0x7fffffff
	#define pb push_back
	#define DEBUG cout<<"You are right,but you are wrong"<<'\n'
	#define END cout<<"You are right,but you are right now"<<'\n'
	#define pii pair<ull,ull>
	#define MP make_pair
	#define fi first
	#define se second
	#define S set<pii >::iterator
	const ull base1=114,base2=1021;
	char ch[N];
	int n,s[N],mx,num;
	set<pii > FNN;
	namespace Edge{
		int h[N],etot,fa[N],sz[N],dson[N],dfn[N],dcnt,DFS[N];
		int val[N];
		struct edge{int to,nxt;}e[N<<1];
		il vd lj(int u,int v){e[++etot]=(edge){v,h[u]},h[u]=etot;}
		vd dfs1(int u){
			sz[u]=1;dfn[u]=++dcnt;DFS[dcnt]=u;
			for(int i=h[u];i;i=e[i].nxt){
				int v=e[i].to;
				if(v==fa[u]) continue;
				fa[v]=u;dfs1(v);sz[u]+=sz[v];
				if(sz[v]>sz[dson[u]]) dson[u]=v;
			}
		}
		vd get_hash(int u,set<pii > &now){
			now.insert(MP(val[u],val[u]));
			set<pii > tmp;
			for(int i=h[u];i;i=e[i].nxt){
				int v=e[i].to;
				if(v==fa[u]) continue;
				get_hash(v,tmp);
				for(S it=tmp.begin();it!=tmp.end();++it) now.insert(MP((*it).fi*base1+val[u],(*it).se*base2+val[u]));
				tmp.clear();
			}
		}
		vd dfs2(int u,set<pii > &now){
//			cout<<u<<'\n';
			now.insert(MP(val[u],val[u]));
			set<pii > tmp;
			for(int i=h[u];i;i=e[i].nxt){
				int v=e[i].to;
				if(v==fa[u]||v==dson[u]) continue;
				dfs2(v,tmp);
				tmp.clear();
			}
			if(dson[u]){
				dfs2(dson[u],tmp);
				for(S it=tmp.begin();it!=tmp.end();++it){
					pii pos=MP((*it).fi*base1+val[u],(*it).se*base2+val[u]);
					now.insert(pos);
				}
				tmp.clear();
			}
			for(int i=h[u];i;i=e[i].nxt){
				int v=e[i].to;
				if(v==fa[u]||v==dson[u]) continue;
				get_hash(v,tmp);
				for(S it=tmp.begin();it!=tmp.end();++it){
					pii pos=MP((*it).fi*base1+val[u],(*it).se*base2+val[u]);
					now.insert(pos);
				}
				tmp.clear();
			}
//			cout<<"u now "<<u<<' '<<now.size()<<'\n';
			ll point=1ll*now.size()+1ll*s[u];
			if(point>mx){
				mx=point;
				num=1;
			}else if(point==mx) ++num;
		}
	}using namespace Edge;
	signed main(){
		cin>>n;
		for(int i=1;i<=n;i++) cin>>s[i];
		cin>>ch;
		for(int i=1;i<=n;i++) val[i]=ch[i-1]-'a'+1;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			lj(u,v),lj(v,u);
		}
		dfs1(1);
		dfs2(1,FNN);
		cout<<mx<<'\n'<<num<<'\n';
		return 0;
	}
}
signed main(){
	TYX_YNXK::main();
	return 0;
}
2024/3/4 10:30
加载中...