結果

問題 No.1103 Directed Length Sum
ユーザー 👑 kmjpkmjp
提出日時 2020-07-03 23:43:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,498 ms / 3,000 ms
コード長 1,262 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 208,660 KB
実行使用メモリ 129,276 KB
最終ジャッジ日時 2023-10-17 06:27:06
合計ジャッジ時間 14,396 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
55,876 KB
testcase_01 AC 22 ms
55,876 KB
testcase_02 AC 388 ms
129,276 KB
testcase_03 AC 276 ms
103,508 KB
testcase_04 AC 791 ms
93,128 KB
testcase_05 AC 1,498 ms
114,868 KB
testcase_06 AC 468 ms
82,104 KB
testcase_07 AC 80 ms
60,928 KB
testcase_08 AC 132 ms
63,496 KB
testcase_09 AC 55 ms
59,060 KB
testcase_10 AC 204 ms
68,428 KB
testcase_11 AC 878 ms
95,360 KB
testcase_12 AC 481 ms
82,496 KB
testcase_13 AC 214 ms
68,716 KB
testcase_14 AC 45 ms
58,268 KB
testcase_15 AC 355 ms
78,228 KB
testcase_16 AC 1,010 ms
99,636 KB
testcase_17 AC 1,054 ms
102,096 KB
testcase_18 AC 205 ms
68,376 KB
testcase_19 AC 910 ms
96,224 KB
testcase_20 AC 66 ms
59,728 KB
testcase_21 AC 118 ms
62,868 KB
testcase_22 AC 717 ms
90,012 KB
testcase_23 AC 383 ms
80,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#undef _P
#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 ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
vector<int> E[2][1010101];
int in[2][1010101];
int C[2][1010101];
const ll mo=1000000007;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		x--,y--;
		E[0][x].push_back(y);
		E[1][y].push_back(x);
		in[0][y]++;
		in[1][x]++;
	}
	FOR(i,2) {
		queue<int> Q;
		FOR(j,N) if(in[i][j]==0) Q.push(j);
		while(Q.size()) {
			x=Q.front();
			Q.pop();
			C[i][x]++;
			FORR(e,E[i][x]) {
				C[i][e]+=C[i][x];
				in[i][e]--;
				if(in[i][e]==0) Q.push(e);
			}
		}
	}
	ll ret=0;
	FOR(i,N) FORR(e,E[0][i]) ret+=1LL*C[0][i]*C[1][e]%mo;
	cout<<ret%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;
}
0