萌新求助
查看原帖
萌新求助
91374
小仓朝阳楼主2020/11/30 07:17

各位神仙好啊,萌新问个问题:

这题不知道为什么一直80分,我用图做的,不是tle,是wa几个点。

思路:用map存下标,然后找每个入度为0的点跑dfs,记录深度,最后输出。

#include<bits/stdc++.h>

#define ll long long
#define re register int
#define For(i,a,b) for(ll i=(a); i<=(b); i++)
const int INF=1<<30;
const int mod=1e9+3;
#define int unsigned ll 
using namespace std;
template <typename T>
inline void read(T &x) { 
	x = 0;
	ll f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
			ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch ^ 48);
		ch = getchar();
	}
	x *= f;
	return;
}
template <typename T>
inline void write(T x){
	if(x < 0) {
		putchar('-');
		x = -x;
	}
	if(x > 9)
		write(x/10); 
	putchar(x % 10 + '0');
	return;
}
struct node{
	int next,to;
}you[1000005];
int head[100005],tot;
map<unsigned ll,unsigned ll> mp;
inline void add(int u,int v){
	tot++;
	you[tot].to=v;
	you[tot].next=head[u];
	head[u]=tot;
}
int ans=0,F[100005],Dep[100005],val[100005],ttt[100005],cnt=0;
int ru[100005],chu[100005];
inline void dfs(int u,int fa,int dep){
	ans=max(dep,ans);
	F[u]=fa;
	Dep[u]=dep;
	for(int i=head[u];i;i=you[i].next){
		int to=you[i].to;
		if(F[to]!=0) continue;
		dfs(to,u,dep+1);
	}
}
signed main(){
	#ifndef ONLINE_JUDGE
		freopen("a.in","r",stdin);
		freopen("a.out","w",stdout);
	#endif
	int n;
	read(n);
	For(i,1,n){
		read(val[i]);
		mp[val[i]]=i;
	}
	For(i,1,n){
		if(val[i]%3==0){
			if(mp[val[i]/3]>0){
				add(i,mp[val[i]/3]);
				ru[mp[val[i]/3]]++;
				chu[i]++;
			}
		}
		if(mp[val[i]*2]>0){
			add(i,mp[val[i]*2]);
			ru[mp[val[i]*2]]++;
			chu[i]++;
		}
	}
	For(i,1,n){
		if(ru[i]==0&&chu[i]>0){
			dfs(i,0,1);
		}
	}
	write(ans);
	puts("");
	For(i,1,n){
		if(Dep[i]==ans){
			int o=i;
			while(F[o]!=0){
				ttt[++cnt]=val[o];
				o=F[o];
			}
			ttt[++cnt]=val[o];
		}
	}
	for(int i=cnt;i>=1;i--){
		cout<<ttt[i]<<" ";
	}



}

2020/11/30 07:17
加载中...