結果
| 問題 |
No.399 動的な領主
|
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2016-07-16 14:16:52 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 131 ms / 2,000 ms |
| コード長 | 4,936 bytes |
| コンパイル時間 | 2,604 ms |
| コンパイル使用メモリ | 178,828 KB |
| 実行使用メモリ | 23,968 KB |
| 最終ジャッジ日時 | 2024-11-07 19:26:32 |
| 合計ジャッジ時間 | 4,221 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:172:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
172 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:176:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
176 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:184:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
184 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
main.cpp:187:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
187 | scanf("%d %d", &A, &B);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
struct CumulativeSum
{
vector< int > data;
CumulativeSum(int sz): data(++sz, 0) {};
inline void add(int k, int x)
{
data[k] += x;
}
void build()
{
for(int i = 1; i < data.size(); i++) {
data[i] += data[i - 1];
}
}
inline int query(int k)
{
return(data[k]);
}
};
struct CentroidPathDecomposition
{
struct Centroid
{
int ParIndex, ParDepth, Deep;
vector< int > node;
inline int size()
{
return(node.size());
}
inline int &operator[](int k)
{
return(node[k]);
}
inline pair< int, int > Up()
{
return(make_pair(ParIndex, ParDepth));
}
};
vector< vector< int > > graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize()
{
stack< pair< int, int > > s;
s.push({0, -1});
while(!s.empty()) {
auto p = s.top(); s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto& to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto& to : graph[p.first]) {
if(p.second != to) s.push({to, p.first});
}
}
}
}
void BuildPath()
{
stack< pair< int, int > > s;
Centroids.push_back((Centroid){-1, -1, 0});
s.push({0, -1});
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top(); s.pop();
TreeDepth[p.first] = Centroids[TreeIndex[p.first]].size();
for(auto& to : graph[p.first]) {
if(p.second != to) {
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = Centroids.size();
Centroids.push_back((Centroid){TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1});
}
s.push({to, p.first});
}
}
Centroids[TreeIndex[p.first]].node.push_back(p.first);
}
}
void AddEdge(int x, int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
void Build()
{
BuildSubTreeSize();
BuildPath();
}
inline int size()
{
return(Centroids.size());
}
inline pair< int, int > Information(int idx)
{
return(make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k)
{
return(Centroids[k]);
}
inline int LCA(int a, int b) // これを流用する
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return(Centroids[TreeIdxA][TreeDepthA]);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
inline void AddPath(int a, int b);
};
vector< CumulativeSum > sum;
inline void CentroidPathDecomposition::AddPath(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
sum[TreeIdxA].add(0, 1);
sum[TreeIdxA].add(TreeDepthA + 1, -1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
sum[TreeIdxB].add(0, 1);
sum[TreeIdxB].add(TreeDepthB + 1, -1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
sum[TreeIdxA].add(TreeDepthA, 1);
sum[TreeIdxB].add(TreeDepthB + 1, -1);
}
int main()
{
int N, Q;
scanf("%d", &N);
CentroidPathDecomposition tree(N);
for(int i = 0; i < N - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
tree.AddEdge(--u, --v);
}
tree.Build();
for(int i = 0; i < tree.size(); i++) {
sum.emplace_back(CumulativeSum(tree[i].size() + 1));
}
scanf("%d", &Q);
while(Q--) {
int A, B;
scanf("%d %d", &A, &B);
tree.AddPath(--A, --B);
}
long long ret = 0;
for(int i = 0; i < tree.size(); i++) {
sum[i].build();
for(int j = 0; j < tree[i].size(); j++) {
int res = sum[i].query(j);
ret += 1LL * res * (res + 1) / 2;
}
}
printf("%lld\n", ret);
}
ei1333333