Submission #18281109


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
vector<int> E[402020];
const ll mo=1000000007;
int C[404040];
map<int,int> MC[404040];
int ON;
ll p2[505050];

pair<int,int> farthest(int cur,int pre,int d,vector<int>& D) {
	D[cur]=d;
	pair<int,int> r={d,cur};
	FORR(e,E[cur]) if(e!=pre) r=max(r, farthest(e,cur,d+1,D));
	return r;
}

pair<int,vector<int>> diameter() { // diameter,center
	vector<int> D[2];
	D[0].resize(N);
	D[1].resize(N);
	auto v1=farthest(0,0,0,D[0]);
	auto v2=farthest(v1.second,v1.second,0,D[0]);
	farthest(v2.second,v2.second,0,D[1]);
	pair<int,vector<int>> R;
	R.first = v2.first;
	for(int i=N-1;i>=0;i--) if(D[0][i]+D[1][i]==R.first && abs(D[0][i]-D[1][i])<=1) R.second.push_back(i);

	return R;
}

pair<int,int> hoge(int cur,int pre,int d,int id) {
	pair<int,int> p={d,cur};
	if(cur<ON) {
		C[cur]++;
		MC[id][d]++;
	}
	FORR(e,E[cur]) if(e!=pre) {
		p=max(p,hoge(e,cur,d+1,id));
		C[cur]+=C[e];
	}
	return p;
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	p2[0]=1;
	FOR(i,404040) p2[i+1]=p2[i]*2%mo;
	
	cin>>N;
	ON=N;
	int NE=N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(NE);
		E[NE].push_back(x-1);
		E[y-1].push_back(NE);
		E[NE].push_back(y-1);
		NE++;
	}
	N=NE;
	
	auto p=diameter();
	assert(p.first%2==0);
	int root=p.second[0];
	vector<vector<int>> D;
	int ma=0;
	int num=root<ON;
	int nd=0;
	FORR(e,E[root]) {
		auto q=hoge(e,root,1,e);
		if(q.first==p.first/2&&D.size()<2) {
			D.push_back({e,q.second});
			MC[e][q.first]--;
		}
		else {
			if(q.first==p.first) nd++;
			ma=max(ma,q.first);
			num+=C[e];
		}
	}
	
	if(nd) {
		ll ret=p.first/2;
		FOR(i,ON) ret=ret*2%mo;
		cout<<ret<<endl;
		return;
	}
	
	//same
	ll ret=p.first/2;
	FOR(i,ON-1) ret=ret*2%mo;
	ll ret2=0;
	int cand=C[D[0][0]]+C[D[1][0]]-2;
	int lef=ON-cand-2;
	for(i=404040;i>=0;i--) {
		x=MC[D[0][0]][i]+MC[D[1][0]][i];
		if(x==0) continue;
		cand-=x;
		int tl=(max(i,ma)+p.first/2)/2*2; //siro kuro
		(ret2+=tl*(p2[x]-1)%mo*p2[cand])%=mo;
	}
	ret2+=(ma+p.first/2)/2*2;
	ret2=ret2*p2[lef]%mo;
	
	
	cout<<(ret+ret2)%mo<<endl;
	
	
	
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}

Submission Info

Submission Time
Task F - Paint Tree
User kmjp
Language C++ (GCC 9.2.1)
Score 0
Code Size 2943 Byte
Status WA
Exec Time 641 ms
Memory 121656 KB

Compile Error

./Main.cpp: In function ‘void solve()’:
./Main.cpp:60:8: warning: unused variable ‘j’ [-Wunused-variable]
   60 |  int i,j,k,l,r,x,y; string s;
      |        ^
./Main.cpp:60:10: warning: unused variable ‘k’ [-Wunused-variable]
   60 |  int i,j,k,l,r,x,y; string s;
      |          ^
./Main.cpp:60:12: warning: unused variable ‘l’ [-Wunused-variable]
   60 |  int i,j,k,l,r,x,y; string s;
      |            ^
./Main.cpp:60:14: warning: unused variable ‘r’ [-Wunused-variable]
   60 |  int i,j,k,l,r,x,y; string s;
      |              ^
./Main.cpp: In function ‘int main(int, char**)’:
./Main.cpp:6:28: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
    6 | #define FOR(x,to) for(x=0;x<(to);x++)
      |                            ^
./Main.cpp:134:38: note: in expansion of macro ‘FOR’
  134 |  FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
      |                                      ^~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 3
AC × 32
WA × 11
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
random_01.txt AC 252 ms 72936 KB
random_02.txt WA 514 ms 87288 KB
random_03.txt WA 329 ms 79428 KB
random_04.txt WA 242 ms 73520 KB
random_05.txt WA 413 ms 84244 KB
random_06.txt WA 235 ms 73568 KB
random_07.txt WA 370 ms 81892 KB
random_08.txt AC 268 ms 75936 KB
random_09.txt WA 548 ms 86624 KB
random_10.txt AC 263 ms 75948 KB
random_11.txt AC 256 ms 73028 KB
random_12.txt WA 512 ms 86908 KB
random_13.txt WA 546 ms 86336 KB
random_14.txt WA 307 ms 78136 KB
random_15.txt WA 444 ms 84148 KB
random_16.txt AC 308 ms 91756 KB
random_17.txt AC 641 ms 121656 KB
random_18.txt AC 270 ms 83148 KB
random_19.txt AC 620 ms 121576 KB
random_20.txt AC 391 ms 100364 KB
random_21.txt AC 252 ms 76660 KB
random_22.txt AC 378 ms 90404 KB
random_23.txt AC 273 ms 76648 KB
random_24.txt AC 447 ms 95964 KB
random_25.txt AC 255 ms 76892 KB
random_26.txt AC 314 ms 89324 KB
random_27.txt AC 250 ms 75688 KB
random_28.txt AC 339 ms 93572 KB
random_29.txt AC 282 ms 83568 KB
random_30.txt AC 349 ms 93624 KB
random_31.txt AC 291 ms 79652 KB
random_32.txt AC 255 ms 78088 KB
random_33.txt AC 351 ms 87904 KB
random_34.txt AC 240 ms 74176 KB
random_35.txt AC 352 ms 87844 KB
random_36.txt AC 322 ms 82012 KB
random_37.txt AC 361 ms 91872 KB
random_38.txt AC 255 ms 77964 KB
random_39.txt AC 446 ms 91716 KB
random_40.txt AC 307 ms 82076 KB
sample_01.txt AC 249 ms 72892 KB
sample_02.txt AC 234 ms 72940 KB
sample_03.txt AC 233 ms 72932 KB