이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int int64_t //be careful about this
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define SZ(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a, b, sizeof(a))
template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin.exceptions(cin.failbit);
cout.precision(15); cout << fixed;
if(SZ(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const long double PI = acos((long double)-1);
struct custom_hash { /// use most bits rather than just the lowest ones
const uint64_t C = (long long)(2e18 * PI) + 71; // large odd number
const int RANDOM = rng();
long long operator()(long long x) const { /// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
return __builtin_bswap64((x^RANDOM)*C); }
};
template<class K,class V> using hash_table = gp_hash_table<K,V,custom_hash>;
const int INF = 1e16;
struct edge {
int node, weight;
edge(int _node, int _weight) : node(_node), weight(_weight) {}
};
struct centroid_decomposition {
#undef int
int N;
vector<vector<edge>> adj;
vector<int> subtree_size;
// parent of a node in centroid tree.
vector<int> centroid_parent;
vector<int> node_list;
#define int int64_t
// gives the distance of each node to its descendants in centroid tree.
vector<hash_table<int, int>> dis; // from root to node.
void init(int _N) {
N = _N;
adj.assign(N, {});
subtree_size.resize(N);
centroid_parent.assign(N, -1);
dis.resize(N);
}
void add_edge(int u, int v, int w) {
assert(u != v);
adj[u].emplace_back(edge(v,w));
adj[v].emplace_back(edge(u,w));
}
// Erasing edges is O(number of nodes remaining) which is fine within our decomposition.
void erase_edge(int from, int to) {
for(edge &e : adj[from]) {
if(e.node == to) {
swap(e, adj[from].back());
adj[from].pop_back();
return;
}
}
assert(false);
}
int dfs(int node, int weight = -1, int parent = -1, int root = -1) {
if(parent < 0) {
root = node;
node_list.clear();
}
if(parent < 0){
dis[root][node] = 0;
}else{
dis[root][node] = weight + dis[root][parent];
}
subtree_size[node] = 1;
node_list.push_back(node);
for(edge &e : adj[node]) {
if(e.node != parent) {
subtree_size[node] += dfs(e.node, e.weight, node, parent < 0 ? node : root);
}
}
return subtree_size[node];
}
int centroid(int root) {
int n = dfs(root);
bool found;
// Repeatedly move to the subtree that is at least half of the tree, if such a subtree exists.
do {
found = false;
for(edge &e : adj[root]){
if(subtree_size[e.node] < subtree_size[root] && 2 * subtree_size[e.node] >= n) {
root = e.node;
found = true;
break;
}
}
} while(found);
return root;
}
// centroid parent of root of centroid tree is -1
void solve(int root) {
root = centroid(root);
dfs(root);
for(int node : node_list){
if(node != root){
centroid_parent[node] = root;
}
}
for(edge &e : adj[root]){
erase_edge(e.node, root);
}
// Recurse after solving root, so that edge erasures don't cause incorrect results.
for(edge &e : adj[root]){
solve(e.node);
}
}
void clear(){
adj.clear();
subtree_size.clear();
}
}cd;
vt<int> ans;
void turn_on(int _i){
for(int i = _i; i >= 0; i = cd.centroid_parent[i]){
ckmin(ans[i],cd.dis[i][_i]);
}
}
void turn_off(int _i){
for(int i = _i; i >= 0; i = cd.centroid_parent[i]){
ans[i] = INF;
}
}
#undef int
void Init(int N, int A[], int B[], int D[]) {
#define int int64_t
cd.init(N);
ans.assign(N,INF);
f(e,0,N-1){
cd.add_edge(A[e],B[e],D[e]);
}
cd.solve(0);
cd.clear();
return;
}
#undef int
long long Query(int S, int X[], int T, int Y[]) {
#define int int64_t
f(i,0,S) turn_on(X[i]);
int res = INF;
f(i,0,T){
for(int j = Y[i]; j >= 0; j = cd.centroid_parent[j]){
ckmin(res, cd.dis[j][Y[i]] + ans[j]);
}
}
f(i,0,S) turn_off(X[i]);
return res;
}
#undef int
/*
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
static int N, Q;
static int A[MAX_N], B[MAX_N], D[MAX_N];
static int S[MAX_N];
static int T[MAX_N];
static int X[MAX_SUM_ST];
static int Y[MAX_SUM_ST];
static int Qx[MAX_N];
static int Qy[MAX_N];
int main() {
int i, j, k;
int STop, TTop;
if (2 != scanf("%d%d", &N, &Q)) {
fprintf(stderr, "error: cannot read N and Q.\n");
exit(1);
}
if (!(2 <= N && N <= MAX_N)) {
fprintf(stderr, "error: N is out of bounds.\n");
exit(1);
}
if (!(1 <= Q && Q <= MAX_Q)) {
fprintf(stderr, "error: Q is out of bounds.\n");
exit(1);
}
for (i = 0; i < N - 1; ++i) {
if (1 != scanf("%d", &A[i])) {
fprintf(stderr, "error: cannot read A[%d].\n", i);
exit(1);
}
if (!(0 <= A[i] && A[i] <= N - 1)) {
fprintf(stderr, "error: A[%d] is out of bounds.\n", i);
exit(1);
}
if (1 != scanf("%d", &B[i])) {
fprintf(stderr, "error: cannot read B[%d].\n", i);
exit(1);
}
if (!(0 <= B[i] && B[i] <= N - 1)) {
fprintf(stderr, "error: B[%d] is out of bounds.\n", i);
exit(1);
}
if (A[i] == B[i]) {
fprintf(stderr, "error: B[%d] is equal to A[%d].\n", i, i);
exit(1);
}
if (1 != scanf("%d", &D[i])) {
fprintf(stderr, "error: cannot read D[%d].\n", i);
exit(1);
}
if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
fprintf(stderr, "error: D[%d] is out of bounds.\n", i);
exit(1);
}
}
STop = 0;
TTop = 0;
for (j = 0; j < Q; ++j) {
if (2 != scanf("%d%d", &S[j], &T[j])) {
fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
exit(1);
}
if(STop + S[j] > MAX_SUM_ST) {
fprintf(stderr, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
exit(1);
}
if(TTop + T[j] > MAX_SUM_ST) {
fprintf(stderr, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
exit(1);
}
for (k = 0; k < S[j]; ++k, ++STop) {
if (1 != scanf("%d", &X[STop])) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= X[STop] && X[STop] <= N - 1)) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
}
for (k = 0; k < T[j]; ++k, ++TTop) {
if (1 != scanf("%d", &Y[TTop])) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
}
}
STop = 0;
TTop = 0;
Init(N, A, B, D);
for (j = 0; j < Q; ++j) {
for (k = 0; k < S[j]; k++) {
Qx[k] = X[STop++];
}
for (k = 0; k < T[j]; k++) {
Qy[k] = Y[TTop++];
}
printf("%lld\n", Query(S[j], Qx, T[j], Qy));
}
return 0;
}
*/
컴파일 시 표준 에러 (stderr) 메시지
factories.cpp: In function 'void setIO(std::string)':
factories.cpp:85:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:86:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |