結果

問題 No.763 Noelちゃんと木遊び
ユーザー tnakao0123
提出日時 2018-12-16 20:24:17
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 91,456 KB
実行使用メモリ 10,692 KB
最終ジャッジ日時 2024-09-25 06:36:16
合計ジャッジ時間 2,419 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:47:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:50:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |     scanf("%d%d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

/* -*- coding: utf-8 -*-
*
* 763.cc: No.763 Noel - yukicoder
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 100000;
/* typedef */
typedef vector<int> vi;
typedef queue<int> qi;
/* global variables */
vi nbrs[MAX_N];
int ps[MAX_N], cs[MAX_N], dp[MAX_N][2];
/* subroutines */
/* main */
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
nbrs[u].push_back(v);
nbrs[v].push_back(u);
}
ps[0] = -1;
qi q;
q.push(0);
while (! q.empty()) {
int u = q.front(); q.pop();
int &up = ps[u];
vi &nbru = nbrs[u];
for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
int &v = *vit;
if (v != up) {
cs[u]++;
ps[v] = u;
q.push(v);
}
}
}
for (int i = 0; i < n; i++)
if (cs[i] == 0) q.push(i);
while (! q.empty()) {
int u = q.front(); q.pop();
dp[u][1]++;
int &up = ps[u];
if (up >= 0) {
dp[up][0] += max(dp[u][0], dp[u][1]);
dp[up][1] += max(dp[u][0], dp[u][1] - 1);
if (--cs[up] == 0) q.push(up);
}
}
printf("%d\n", max(dp[0][0], dp[0][1]));
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0