Submission #44099745


Source Code Expand

#include<cstdio>
#include<cstring>
using namespace std;
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
// #define add(a,b) (a+=b)>=mod&&(a-=mod)
#define sub(a,b) (a-=b)<0&&(a+=mod)
const int N=200010,M=400010;
namespace IO{
	const char fg=' ';
	int len=0;
	char ibuf[(1<<20)+1],*iS,*iT,out[(1<<25)+1],ar[50];
	#define gh()\
	(iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),\
	(iS==iT?EOF:*iS++):*iS++)
	#define putc(ch) out[len++]=ch
	void read(){}
	template<typename Type,typename...Types>
	void read(Type&x,Types&...xs){
		x=0;
		char ch=gh();
		char t=0;
		while(ch<'0'||ch>'9')t|=ch=='-',ch=gh();
		while(ch>='0'&&ch<='9')x=x*10+(ch^48),ch=gh();
		x=t?-x:x;
		read(xs...);
	}
	template<typename Type>
	void write(Type&x){
		int tot=0;
		if(!x)putc('0');
		if(x<0)putc('-'),x=-x;
		while(x)ar[++tot]=x%10+'0',x/=10;
		for(int i=tot;i;--i)putc(ar[i]);
		putc(fg);
	}
	void flush(){
		fwrite(out,1,len,stdout);
		len=0;
	}
}
typedef long long ll;
using namespace IO;
int n,e[M],h[N],ne[M],sz,idx;
ll ans;
void add(int a,int b){
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int x,int fa){
	int sz=1;
	for(int i=h[x];~i;i=ne[i]){
		int j=e[i];
		if(j==fa)continue;
		ll tmp=dfs(j, x);
		sz+=tmp;
		ans+=tmp*(n-tmp);
		// printf("j:%d,ans=%lld,ans=%lld\n",j,tmp*(n-tmp),ans);
	}
	return sz;
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
	// freopen("1.out","w",stdout);
	#endif
	// Insert Code Here
	// ios::sync_with_stdio(0);
	// cin.tie(0);
	// cout.tie(0);
	read(n);
	memset(h,-1,n*4+4);
	E(i, n-1){
		int a,b;
		read(a,b);
		add(a,b),add(b,a);
	}
	dfs(1,-1);
	ans=ans-(n*(n-1ll)>>1);
	// printf("%lld\n",ans);
	ans=n*(n-1ll)*(n-2)/6-ans;
	write(ans);
	flush();
	return 0;
}

Submission Info

Submission Time
Task G - Avoid Straight Line
User WUSICHENG
Language C++ (GCC 9.2.1)
Score 550
Code Size 1993 Byte
Status AC
Exec Time 43 ms
Memory 15636 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 3
AC × 40
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt
Case Name Status Exec Time Memory
sample00.txt AC 6 ms 1420 KiB
sample01.txt AC 1 ms 1424 KiB
sample02.txt AC 1 ms 1560 KiB
testcase00.txt AC 1 ms 1484 KiB
testcase01.txt AC 2 ms 1488 KiB
testcase02.txt AC 1 ms 1556 KiB
testcase03.txt AC 28 ms 10544 KiB
testcase04.txt AC 41 ms 12908 KiB
testcase05.txt AC 27 ms 10204 KiB
testcase06.txt AC 43 ms 15636 KiB
testcase07.txt AC 14 ms 4940 KiB
testcase08.txt AC 17 ms 6336 KiB
testcase09.txt AC 20 ms 5240 KiB
testcase10.txt AC 19 ms 6276 KiB
testcase11.txt AC 16 ms 4928 KiB
testcase12.txt AC 22 ms 6336 KiB
testcase13.txt AC 19 ms 5536 KiB
testcase14.txt AC 27 ms 6396 KiB
testcase15.txt AC 22 ms 5188 KiB
testcase16.txt AC 32 ms 6468 KiB
testcase17.txt AC 15 ms 4720 KiB
testcase18.txt AC 23 ms 6332 KiB
testcase19.txt AC 25 ms 5388 KiB
testcase20.txt AC 25 ms 6332 KiB
testcase21.txt AC 14 ms 4644 KiB
testcase22.txt AC 23 ms 6464 KiB
testcase23.txt AC 20 ms 7876 KiB
testcase24.txt AC 35 ms 11504 KiB
testcase25.txt AC 27 ms 9992 KiB
testcase26.txt AC 34 ms 9448 KiB
testcase27.txt AC 23 ms 5704 KiB
testcase28.txt AC 31 ms 6400 KiB
testcase29.txt AC 20 ms 5012 KiB
testcase30.txt AC 28 ms 6464 KiB
testcase31.txt AC 17 ms 4768 KiB
testcase32.txt AC 31 ms 6276 KiB
testcase33.txt AC 31 ms 6288 KiB
testcase34.txt AC 32 ms 6400 KiB
testcase35.txt AC 25 ms 6144 KiB
testcase36.txt AC 33 ms 6324 KiB