이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (stderr) 메시지
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 |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |