QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#21253 | #1265. Total Eclipse | FudanU1# | AC ✓ | 802ms | 13408kb | C++17 | 1.3kb | 2022-03-04 13:54:12 | 2022-05-08 02:47:44 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
int i , x;
} s[120000];
int n , m;
vector < int > g[120000];
int c[120000];
int vis[120000];
long long ans;
void clear () {
int i;
for ( i = 1 ; i <= n ; i++ ) {
g[i].clear ();
vis[i] = c[i] = 0;
}
ans = 0;
}
bool cmp ( node x1 , node x2 ) {
return x1.x > x2.x;
}
int find ( int x ) {
int i , t;
for ( i = x ; c[i] > 0 ; i = c[i] ) ;
while ( c[x] > 0 ) {
t = c[x];
c[x] = i;
x = t;
}
return i;
}
void Union ( int x , int y ) {
c[y] = x;
}
void work () {
int i , u , v;
scanf ( "%d%d" , &n , &m );
for ( i = 1 ; i <= n ; i++ ) {
scanf ( "%d" , &s[i].x );
s[i].i = i;
}
for ( i = 1 ; i <= m ; i++ ) {
scanf ( "%d%d" , &u , &v );
g[u].push_back ( v );
g[v].push_back ( u );
}
sort ( s + 1 , s + 1 + n , cmp );
ans = 0;
for ( i = 1 ; i <= n ; i++ ) {
c[i] = -1;
}
for ( i = 1 ; i <= n ; i++ ) {
ans += s[i].x;
vis[s[i].i] = 1;
for ( auto j : g[s[i].i] ) {
if ( vis[j] == 0 ) continue;
if ( find ( j ) == find ( s[i].i ) ) {
continue;
}
ans -= s[i].x;
Union ( find ( s[i].i ) , find ( j ) );
}
}
printf ( "%lld\n" , ans );
}
int main () {
int t;
scanf ( "%d" , &t );
while ( t-- ) {
work ();
clear ();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6220kb
input:
1 3 2 3 2 3 1 2 2 3
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 802ms
memory: 13408kb
input:
10 90000 89999 29695126 280198164 879119046 430007344 949559367 73085127 889966040 364820814 284458985 931692411 799753826 296370022 333668874 420472873 151595337 343496667 39363958 831424981 863897406 650573429 217091529 533448388 789707546 749864138 599409565 909987504 320920370 476338054 11898738...
output:
15024049952546 16659615874110 400000005 400000003 5601933778666 6160080698685 5988219861596 6982527535131 6237837736440 6956893227831
result:
ok 10 lines