結果
| 問題 |
No.2427 Tree Distance Two
|
| コンテスト | |
| ユーザー |
kaityo_17
|
| 提出日時 | 2023-08-18 21:45:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,487 bytes |
| コンパイル時間 | 1,990 ms |
| コンパイル使用メモリ | 176,744 KB |
| 実行使用メモリ | 37,572 KB |
| 最終ジャッジ日時 | 2024-11-28 06:25:51 |
| 合計ジャッジ時間 | 69,262 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 TLE * 21 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i = a; i < n; i++)
#define yes (cout << "Yes" << endl)
#define YES (cout << "YES" << endl)
#define no (cout << "No" << endl)
#define NO (cout << "NO" << endl)
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vd = vector<double>;
using vl = vector<long long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using Graph = vector<vector<int>>;
const int mod = 998244353;
const int MOD = 1000000007;
const double pi = 3.141592653589793238;
const string abc = "abcdefghijklmnopqrstuvwxyz";
int dight_sum(int t) {
int ans = 0;
while(t >= 10) {
ans += t % 10;
t /= 10;
}
ans += t;
return ans;
}
vector<bool> seen;
void dfs(const Graph &G, int v) {
seen[v] = true; // v を訪問済にする
// v から行ける各頂点 next_v について
for (auto next_v : G[v]) {
if (seen[next_v]) continue; // next_v が探索済だったらスルー
dfs(G, next_v); // 再帰的に探索
}
}
ll gcd(ll a,ll b){
while(b!=0){
a%=b;
swap(a,b);
}
return a;
}
struct unionfind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
unionfind (int N) : par(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
long long factorial(long long k) {
ll ans = 1;
rep(i,1,k + 1) {
ans *= i;
}
return ans;
}
int main () {
cout << fixed << setprecision(10);
int n;
cin >> n;
int m = n -1;
Graph G(n);
rep(i,0,m) {
int a,b;
cin >> a >> b;
a--;
b--;
G[a].pb(b);
G[b].pb(a);
}
rep(i,0,n) {
vector<int> dist(n, -1); // 全頂点を「未訪問」に初期化
queue<int> que;
// 初期条件 (頂点 0 を初期ノードとする)
dist[i] = 0;
que.push(i); // 0 を橙色頂点にする
// BFS 開始 (キューが空になるまで探索を行う)
while (!que.empty()) {
int v = que.front(); // キューから先頭頂点を取り出す
que.pop();
// v から辿れる頂点をすべて調べる
for (int nv : G[v]) {
if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない
// 新たな白色頂点 nv について距離情報を更新してキューに追加する
dist[nv] = dist[v] + 1;
que.push(nv);
}
}
int cnt = 0;
rep(i,0,n) {
if(dist[i] == 2) cnt++;
}
cout << cnt << endl;
}
}
kaityo_17