Submission #1237323

#TimeUsernameProblemLanguageResultExecution timeMemory
1237323vietbachleonkroos2326Two Currencies (JOI23_currencies)C++20
10 / 100
5089 ms66688 KiB
#include<bits/stdc++.h>
#define TIME (1.0* clock()/CLOCKS_PER_SEC)
#define pb push_back
#define eb emplace_back
#define id1 (id<<1)
#define id2 (id<<1)|1
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int>> 
#define vl vector<long long>
#define vll vector <pair<ll,ll>>
#define li pair<long long,int>
#define vil vector <pair<int,ll>>
#define db double
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i <=(b); i++)
#define F0R(i, a) FOR(i, 0, a-1)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a-1)
#define rep(a) F0R(_, a)
#define each(a, x) for (auto &a : x)
#define ALL(x) (x).begin(),(x).end()
#define pq priority_queue <li, vector <li>, greater <li>> 
using namespace std;
const int maxn=1e6;
//const int MOD=1e9+7;
//const int MOD=998244353;
//const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1};
#define int long long

int n,m,q;

int a[maxn],b[maxn],c[maxn];
vector<int> w[maxn];
vector<pair<int,int>> ke[maxn];

int h[maxn];

bool sub1(){
  return(n<=2000 and m<=2000 and q<=2000);
}

int pa[maxn],e[maxn];


void dfssub1(int u,int p){
  for(auto[v,i]:ke[u]){
    if(v==p) continue;
    h[v]=h[u]+1;
    pa[v]=u;
    e[v]=i;
    dfssub1(v,u);
  }
}

int querysub1(int s,int t,int x,int y){
  if(s==t) return x;
  vector<int> vec;

 
  while(h[s] > h[t]){
    each(v, w[e[s]]) vec.pb(v);
    s = pa[s];
  }
  while(h[t] > h[s]){
    each(v, w[e[t]]) vec.pb(v);
    t = pa[t];
  }

  while(s != t){
    each(v, w[e[s]]) vec.pb(v);
    s = pa[s];
    each(v, w[e[t]]) vec.pb(v);
    t = pa[t];
  }

  sort(ALL(vec));

  for (int i = 0; i < vec.size(); ++i) {
      if (vec[i] <= y) {
          y -= vec[i];
      } else if (x > 0) {
          x--;
      } else {
          return -1;
      }
  }

  return x;
}


void solvesub1(){
  dfssub1(1,0);

  while(q--){
    int s,t,x,y;
    cin>>s>>t>>x>>y;
    cout<<querysub1(s,t,x,y)<<"\n";
  }
}

bool sub2(){
  bool check=true;
  for(int i=1;i<m;i++){
    if(c[i]!=c[i+1]) check=false;
  }
  return check;
}

void solvesub2(){

}


void solve(){
  cin>>n>>m>>q;
  for(int i=1;i<n;i++){
    cin>>a[i]>>b[i];
    ke[a[i]].eb(b[i],i);
    ke[b[i]].eb(a[i],i);
  }
  for(int i=1;i<=m;i++){
    int p, ci; 
    cin>>p>>ci;
    c[i] = ci; 
    w[p].push_back(ci);
  }

  solvesub1();
}

signed main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
  if (fopen("TASK.INP", "r")){
      freopen("TASK.INP", "r", stdin);
      freopen("TASK.OUT", "w", stdout);
  }
  int ntest;
  ntest=1;
  //cin>>ntest;
  
  for(int i=1;i<=ntest;i++) solve();
  cerr<<"\n"<<"Time elapsed "<<TIME<<"s.\n";
  return 0;
}

Compilation message (stderr)

currencies.cpp: In function 'int main()':
currencies.cpp:141:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |       freopen("TASK.INP", "r", stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:142:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |       freopen("TASK.OUT", "w", stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...