이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
tomorrow will worse
but the day after tomorrow will be the sunshine..
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up */
//The most CHALISHKANCHIK
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const int N = 1e5+5;
vi g[N];
map<pii, vi> mp;
map<pii, int> mp2;
int t;
int pre[N];
int n, q, timer;
int tin[N], tout[N];
int up[20][N];
int num[N]{};
void dfs(int v, int pr) {
tin[v] = ++timer;
up[0][v] = pr;
for (int i = 1; i <= 17; i++) {
up[i][v] = up[i - 1][ up[i - 1][v] ];
}
for (int to : g[v]) {
if (to == pr) continue;
dfs(to, v);
}
tout[v] = timer;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int i = 17; i >= 0; i--) {
if (!upper(up[i][a], b)) {
a = up[i][a];
}
}
return up[0][a];
}
void sukfe(int v, int pr){
for (int to : g[v]) {
if (to == pr) continue;
num[to] = num[v];
num[to] += mp2[{v, to}];
dfs(to, v);
}
}
void dfs2(int v, int pr){
for (int to : g[v]) {
if (to == pr) continue;
pre[to] = v;
dfs2(to, v);
}
}
void solve(){
int n, m, q;
cin >> n >> m >> q;
vii cui;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
cui.pb({a, b});
}
int j, c;
for(int i = 0; i < m; i++){
cin >> j >> c;j--;
mp[cui[j]].pb(c);
mp[{cui[j].ss, cui[j].ff}].pb(c);
mp2[cui[j]]++;
mp2[{cui[j].ss, cui[j].ff}]++;
}
if(n <= 2000){
vi pas;
while(q--){
int s, x, y;
cin >> s >> t >> x >> y;
dfs2(s, -1);
int r = t;
while(r!=s){
for(auto i:mp[{r, pre[r]}])pas.pb(i);
r = pre[r];
}
sort(all(pas));
for(auto i:pas){
if(i <= y)y -= i;
else x--;
}pas.clear();
cout << (x < 0 ? -1 : x) << '\n';
}
}else{
dfs(1, -1);
sukfe(1, -1);
while(q--){
int a, b, x, y;
cin >> a >> b >> x >> y;
int cnt = num[a] + num[b];
cnt -= num[lca(a, b)] * 2;
int ans = y/c;
cnt -= ans;
if(x - cnt < 0)cout << -1 << '\n';
else cout << x - cnt << '\n';
}
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp:127:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
127 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |