Submission #22681

# Submission time Handle Problem Language Result Execution time Memory
22681 2017-04-30T06:30:03 Z Jongwon Party(#988, gs13105) Logistical Metropolis (KRIII5_LM) C++14
2 / 7
46 ms 1360 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

struct edg
{
    int y, c;
};

struct edg2
{
    int x, y, c;
    bool operator <(const edg2 &a) const
    {
        return c < a.c;
    }
};

std::vector<edg> arr[1001];
std::vector<edg2> sor;

int par[1001];
int rnk[1001];

int root(int x)
{
    return x == par[x] ? x : (par[x] = root(par[x]));
}

bool merge(int x, int y)
{
    x = root(x);
    y = root(y);
    if(x == y)
        return 0;

    if(rnk[x] < rnk[y])
        par[x] = y;
    else
    {
        par[y] = x;
        if(rnk[x] == rnk[y])
            rnk[x]++;
    }
    return 1;
}
    

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int n, m, x, y, c, i, j;
    long long r;
    scanf("%d%d", &n, &m);
    for(i = 0; i<m; i++)
    {
        scanf("%d%d%d", &x, &y, &c);
        arr[x].push_back({ y, c });
        arr[y].push_back({ x, c });
        sor.push_back({ x, y, c });
    }

    sort(sor.begin(), sor.end());

    for(i = 1; i<=n; i++)
    {
        r = 0;
        for(j = 1; j<=n; j++)
        {
            par[j] = j;
            rnk[j] = 0;
        }

        for(edg &e: arr[i])
            if(merge(i, e.y))
                r += e.c;

        for(edg2 &e: sor)
            if(merge(e.x, e.y))
                r += e.c;

        printf("%lld\n", r);
    }

    return 0;
}

Compilation message

LM.cpp: In function 'int main()':
LM.cpp:60:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
                          ^
LM.cpp:63:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &x, &y, &c);
                                    ^
# Verdict Execution time Memory Grader output
1 Correct 43 ms 1356 KB Output is correct
2 Correct 46 ms 1356 KB Output is correct
3 Correct 39 ms 1360 KB Output is correct
4 Correct 43 ms 1356 KB Output is correct
5 Correct 43 ms 1356 KB Output is correct
6 Correct 43 ms 1356 KB Output is correct
7 Correct 39 ms 1356 KB Output is correct
8 Correct 39 ms 1356 KB Output is correct
9 Correct 43 ms 1356 KB Output is correct
10 Correct 39 ms 1356 KB Output is correct
11 Correct 43 ms 1356 KB Output is correct
12 Correct 36 ms 1356 KB Output is correct
13 Correct 39 ms 1356 KB Output is correct
14 Correct 39 ms 1356 KB Output is correct
15 Correct 39 ms 1356 KB Output is correct
16 Correct 43 ms 1356 KB Output is correct
17 Correct 46 ms 1356 KB Output is correct
18 Correct 46 ms 1356 KB Output is correct
19 Correct 46 ms 1356 KB Output is correct
20 Correct 46 ms 1356 KB Output is correct
21 Correct 46 ms 1356 KB Output is correct
22 Correct 43 ms 1356 KB Output is correct
23 Correct 43 ms 1356 KB Output is correct
24 Correct 46 ms 1356 KB Output is correct
25 Correct 0 ms 1208 KB Output is correct
26 Correct 0 ms 1208 KB Output is correct
27 Correct 0 ms 1208 KB Output is correct
28 Correct 9 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1208 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -