結果

問題 No.1103 Directed Length Sum
ユーザー QCFiumQCFium
提出日時 2020-07-14 18:34:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 535 ms / 3,000 ms
コード長 1,515 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 177,720 KB
実行使用メモリ 88,192 KB
最終ジャッジ日時 2024-04-29 01:02:18
合計ジャッジ時間 7,404 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 142 ms
88,192 KB
testcase_03 AC 73 ms
60,032 KB
testcase_04 AC 231 ms
42,624 KB
testcase_05 AC 535 ms
71,296 KB
testcase_06 AC 157 ms
28,928 KB
testcase_07 AC 16 ms
9,344 KB
testcase_08 AC 27 ms
12,544 KB
testcase_09 AC 10 ms
6,912 KB
testcase_10 AC 50 ms
16,000 KB
testcase_11 AC 304 ms
46,080 KB
testcase_12 AC 151 ms
29,184 KB
testcase_13 AC 49 ms
16,256 KB
testcase_14 AC 8 ms
6,144 KB
testcase_15 AC 92 ms
23,296 KB
testcase_16 AC 312 ms
51,328 KB
testcase_17 AC 324 ms
53,376 KB
testcase_18 AC 38 ms
16,000 KB
testcase_19 AC 274 ms
47,104 KB
testcase_20 AC 12 ms
7,936 KB
testcase_21 AC 22 ms
11,648 KB
testcase_22 AC 209 ms
39,040 KB
testcase_23 AC 96 ms
24,704 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:39:15: warning: 'void* memset(void*, int, size_t)' specified size between 18446744071562068017 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
   39 |         memset(used, 0, sizeof(used));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

struct FastIO {
	char buf[20000000];
	char *ptr = buf;
	void read() {
		int size;
		size = fread(buf, 1, sizeof(buf) - 1, stdin);
		buf[size] = '\0';
		ptr = buf;
	}
	FastIO () {
		read();
	}
	void inc() {
		ptr++;
		// if (++ptr == buf + sizeof(buf) || !*ptr) read();
	}
	template<typename Integer> Integer read() {
		bool neg = false;
		Integer res = 0;
		while ((*ptr < '0' || *ptr > '9') && *ptr != '-') inc();
		if (*ptr == '-') neg = true, inc();
		while (*ptr >= '0' && *ptr <= '9') res = res * 10 + *ptr - '0', inc();
		return neg ? -res : res;
	}
} fastio;
#define ri fastio.read<int>
#define rs64 fastio.read<int64_t>


std::vector<std::vector<int> > hen;

int main() {
	int n = ri();
	int degree[n];
	memset(degree, 0, sizeof(degree));
	bool used[n];
	memset(used, 0, sizeof(used));
	std::pair<int, int> hens[n - 1];
	for (int i = 0; i + 1 < n; i++) {
		hens[i].first = ri() - 1;
		hens[i].second = ri() - 1;
		degree[hens[i].first]++;
		used[hens[i].second] = true;
	}
	
	int root = std::find(used, used + n, 0) - used;
	hen.resize(n);
	for (int i = 0; i < n; i++) hen[i].resize(degree[i]);
	for (auto i : hens) hen[i.first][--degree[i.first]] = i.second;
	
	int depth[n];
	depth[root] = 0;
	std::queue<int> que;
	que.push(root);
	int64_t res = 0;
	while (que.size()) {
		int i = que.front();
		que.pop();
		res += (int64_t) depth[i] * (depth[i] + 1) / 2;
		for (auto j : hen[i]) depth[j] = depth[i] + 1, que.push(j);
	}
	printf("%d\n", (int) (res % 1000000007));
	return 0;
}
0