Submission #1017364
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(int i = (l);i < (r);i++)
#define ALL(x) (x).begin(),(x).end()
template<typename T> bool chmax(T& a,const T& b){return a < b ? (a = b,true) : false;}
template<typename T> bool chmin(T& a,const T& b){return b < a ? (a = b,true) : false;}
typedef long long ll;
class UnionFind{
private:
vector<int> parent;
vector<int> rank;
public:
UnionFind(int n)
:parent(n + 1),rank(n + 1,1){
for(int i = 1;i <= n;i++) parent [i] = i;
}
int find(int x){
if(parent [x] == x) return x;
return parent [x] = find(parent [x]);
}
bool connect(int x,int y){
x = find(x),y = find(y);
if(x == y) return false;
if(rank [x] < rank [y]) swap(x,y);
if(rank [x] == rank [y]) rank [x]++;
parent [y] = x;
return true;
}
bool same(int x,int y){
return find(x) == find(y);
}
};
int N,M,Q;
list< pair<int,ll> > edge [4001],used_edge [4001];
struct st{
int a,b;
ll c;
bool operator<(const st& other){
return this->c < other.c;
}
};
ll max_edge [4001] [4001];
ll dfs(int curr,int prev,int s,ll mx)
{
max_edge [s] [curr] = mx;
max_edge [curr] [s] = mx;
for(auto&& it : used_edge [curr]) if(it.first != prev){
dfs(it.first,curr,s,max(mx,it.second));
}
}
int main()
{
scanf("%d%d",&N,&M);
vector<st> A(M);
FOR(i,0,M){
scanf("%d%d%lld",&A [i].a,&A [i].b,&A [i].c);
edge [A [i].a].push_back({A [i].b,A [i].c});
edge [A [i].b].push_back({A [i].a,A [i].c});
}
sort(ALL(A));
scanf("%d",&Q);
vector< pair<int,int> > query(Q);
FOR(i,0,Q){
scanf("%d%d",&query [i].first,&query [i].second);
}
ll ans = 0;
UnionFind uf(N);
FOR(i,0,M){
if(uf.same(A [i].a,A [i].b) == false){
uf.connect(A [i].a,A [i].b);
used_edge [A [i].a].push_back({A [i].b,A [i].c});
used_edge [A [i].b].push_back({A [i].a,A [i].c});
ans += A [i].c;
}
}
for(int i = 1;i <= N;i++){
dfs(i,-1,i,0);
}
FOR(i,0,Q){
printf("%lld\n",ans - max_edge [query [i].first] [query [i].second]);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
A - Graph |
User |
gigime |
Language |
C++14 (GCC 5.4.1) |
Score |
700 |
Code Size |
2098 Byte |
Status |
AC |
Exec Time |
1047 ms |
Memory |
171648 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:58:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&N,&M);
^
./Main.cpp:61:47: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld",&A [i].a,&A [i].b,&A [i].c);
^
./Main.cpp:67:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&Q);
^
./Main.cpp:70:51: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&query [i].first,&query [i].second);
^
Judge Result
Set Name |
Sample |
subtask1 |
subtask2 |
All |
Score / Max Score |
0 / 0 |
200 / 200 |
300 / 300 |
200 / 200 |
Status |
|
|
|
|
Set Name |
Test Cases |
Sample |
sample_1.txt, sample_2.txt |
subtask1 |
sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt |
subtask2 |
sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt |
All |
sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_1.txt |
AC |
3 ms |
384 KB |
sample_2.txt |
AC |
3 ms |
384 KB |
subtask_1_1.txt |
AC |
4 ms |
1024 KB |
subtask_1_10.txt |
AC |
854 ms |
147712 KB |
subtask_1_11.txt |
AC |
3 ms |
384 KB |
subtask_1_2.txt |
AC |
756 ms |
130176 KB |
subtask_1_3.txt |
AC |
999 ms |
169600 KB |
subtask_1_4.txt |
AC |
702 ms |
126208 KB |
subtask_1_5.txt |
AC |
735 ms |
126336 KB |
subtask_1_6.txt |
AC |
785 ms |
136704 KB |
subtask_1_7.txt |
AC |
1010 ms |
169600 KB |
subtask_1_8.txt |
AC |
692 ms |
126208 KB |
subtask_1_9.txt |
AC |
729 ms |
126848 KB |
subtask_2_1.txt |
AC |
1019 ms |
169728 KB |
subtask_2_2.txt |
AC |
1010 ms |
169600 KB |
subtask_2_3.txt |
AC |
1024 ms |
169600 KB |
subtask_2_4.txt |
AC |
1021 ms |
169600 KB |
subtask_2_5.txt |
AC |
701 ms |
126336 KB |
subtask_2_6.txt |
AC |
745 ms |
128000 KB |
subtask_2_7.txt |
AC |
794 ms |
136832 KB |
subtask_2_8.txt |
AC |
1014 ms |
169728 KB |
subtask_3_1.txt |
AC |
1047 ms |
171520 KB |
subtask_3_2.txt |
AC |
1027 ms |
171520 KB |
subtask_3_3.txt |
AC |
747 ms |
128384 KB |
subtask_3_4.txt |
AC |
774 ms |
128896 KB |
subtask_3_5.txt |
AC |
895 ms |
149632 KB |
subtask_3_6.txt |
AC |
966 ms |
160512 KB |
subtask_3_7.txt |
AC |
1043 ms |
171648 KB |
subtask_3_8.txt |
AC |
1024 ms |
171520 KB |