This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 50050
vector<int> ufds(maxn);
int find(int k){
if(ufds[k]!=k){
ufds[k] = find(ufds[k]);
}
return ufds[k];
}
void merge(int a,int b){
int temp1 = find(a);
int temp2 = find(b);
ufds[min(temp1,temp2)] = max(temp1,temp2);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int k,n,m,o;
for(int i=0;i<ufds.size();i++){
ufds[i] = i;
}
cin >> k >> n >> m >> o;
int input1,input2,input3;
vector<int> prefix(maxn,0);
for(int i=0;i<m;i++){
cin >> input1 >> input2 >> input3;
prefix[input1] += input3;
merge(input1,input2);
}
for(int i=1;i<prefix.size();i++){
prefix[i] += prefix[i-1];
}
for(int i=0;i<o;i++){
cin >> input1 >> input2;
int currsum = prefix[input2];
if(input1!=0){
currsum -= prefix[input1-1];
}
if(find(input1)!=find(input2)){
cout << -1 << "\n";
}
else{
cout << currsum << "\n";
}
}
}
Compilation message (stderr)
toll.cpp: In function 'int32_t main()':
toll.cpp:21:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ufds.size();i++){
~^~~~~~~~~~~~
toll.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<prefix.size();i++){
~^~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |