# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
968367 |
2024-04-23T11:04:55 Z |
marvinthang |
Prize (CEOI22_prize) |
C++17 |
|
2103 ms |
540200 KB |
/*************************************
* author: marvinthang *
* created: 23.04.2024 17:01:12 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int LOG = 18;
int n, k, q, t;
vector <bool> choosed;
struct Tree {
int n, rt;
vector <vector <int>> adj;
Tree(int _n = 0) {
n = _n;
adj.resize(n + 1);
FORE(u, 1, n) {
int p; cin >> p;
if (p == -1) rt = u;
else adj[p].push_back(u);
}
}
vector <int> get_first_k(void) {
vector <int> res{rt};
for (int i = 0; (int) res.size() < k; ++i) res.insert(res.end(), ALL(adj[res[i]]));
res.resize(k);
return res;
}
vector <int> get_order(void) {
vector <int> res;
stack <int> st;
st.push(rt);
while (!st.empty()) {
int u = st.top(); st.pop();
if (choosed[u]) res.push_back(u);
for (int v: adj[u]) st.push(v);
}
return res;
}
vector <int> ver, pos, depth, dist;
vector <array <int, LOG>> anc;
vector <vector <int>> tadj;
vector <vector <pair <int, int>>> fadj;
int dfs(int u) {
vector <int> child;
for (int v: adj[u]) {
v = dfs(v);
if (v != -1) child.push_back(v);
}
if (choosed[u] || (int) child.size() > 1) {
pos[u] = ver.size();
ver.push_back(u);
tadj.push_back(child);
return pos[u];
}
if ((int) child.size() == 1) return child[0];
return -1;
}
int get_lca(int u, int v) {
u = pos[u]; v = pos[v];
if (depth[u] < depth[v]) swap(u, v);
REPD(i, LOG) if (depth[anc[u][i]] >= depth[v]) u = anc[u][i];
if (u == v) return u;
REPD(i, LOG) if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
return ver[anc[u][0]];
}
void rebuild(void) {
pos.resize(n + 1);
dfs(rt);
n = ver.size();
depth.resize(n);
anc.resize(n);
queue <int> q;
q.push(n - 1);
anc[n - 1][0] = n - 1;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v: tadj[u]) {
depth[v] = depth[u] + 1;
anc[v][0] = u;
q.push(v);
}
}
FOR(k, 1, LOG) REP(u, n) anc[u][k] = anc[anc[u][k - 1]][k - 1];
fadj.resize(n);
}
void add_edge(int u, int v, int w) {
u = pos[u]; v = pos[v];
fadj[u].emplace_back(w, v);
fadj[v].emplace_back(-w, u);
}
void build_weights(void) {
dist.assign(n, -1);
dist[n - 1] = 0;
queue <int> q;
q.push(n - 1);
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto [w, v]: fadj[u]) if (dist[v] == -1) {
dist[v] = dist[u] + w;
q.push(v);
}
}
}
int get_dist(int u, int v) {
return dist[pos[u]] + dist[pos[v]] - 2 * dist[pos[get_lca(u, v)]];
}
};
void process(void) {
cin >> n >> k >> q >> t;
choosed.resize(n + 1);
Tree first(n), second(n);
vector <int> lst = first.get_first_k();
for (int x: lst) {
choosed[x] = true;
cout << x << ' ';
}
cout << endl;
vector <int> se_order = second.get_order();
first.rebuild();
second.rebuild();
REP(i, k - 1) cout << "? " << se_order[i] << ' ' << se_order[i + 1] << '\n';
cout << "!" << endl;
REP(i, k - 1) {
int u = se_order[i], v = se_order[i + 1];
int l1 = first.get_lca(u, v);
int l2 = second.get_lca(u, v);
int a, b, c, d; cin >> a >> b >> c >> d;
first.add_edge(l1, u, a);
first.add_edge(l1, v, b);
second.add_edge(l2, u, c);
second.add_edge(l2, v, d);
}
assert(false);
first.build_weights();
second.build_weights();
vector <pair <int, int>> res;
while (t--) {
int u, v; cin >> u >> v;
res.emplace_back(first.get_dist(u, v), second.get_dist(u, v));
}
for (auto [u, v]: res) cout << u << ' ' << v << '\n';
cout.flush();
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("prize");
// int t; cin >> t; while (t--)
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:191:2: note: in expansion of macro 'file'
191 | file("prize");
| ^~~~
Main.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:191:2: note: in expansion of macro 'file'
191 | file("prize");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
757 ms |
285560 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
912 ms |
310352 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
611 ms |
230824 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1452 ms |
461888 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2103 ms |
540200 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |