Pagini recente »
Borderou de evaluare (job #100900)
|
Borderou de evaluare (job #786937)
|
Clasament calu_paste_fericit
|
matrix-revolution
|
Cod sursă (job #675962)
Cod sursă (job
#675962)
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
FILE* fin;
char* buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int& n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
}
else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long& n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
}
else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
class OutParser {
private:
FILE* fout;
char* buff;
int sp;
void write_ch(char ch) {
if (sp == 50000) {
fwrite(buff, 1, 50000, fout);
sp = 0;
buff[sp++] = ch;
}
else {
buff[sp++] = ch;
}
}
public:
OutParser(const char* name) {
fout = fopen(name, "w");
buff = new char[50000]();
sp = 0;
}
~OutParser() {
fwrite(buff, 1, sp, fout);
fclose(fout);
}
OutParser& operator << (int vu32) {
if (vu32 <= 9) {
write_ch(vu32 + '0');
}
else {
(*this) << (vu32 / 10);
write_ch(vu32 % 10 + '0');
}
return *this;
}
OutParser& operator << (long long vu64) {
if (vu64 <= 9) {
write_ch(vu64 + '0');
}
else {
(*this) << (vu64 / 10);
write_ch(vu64 % 10 + '0');
}
return *this;
}
OutParser& operator << (char ch) {
write_ch(ch);
return *this;
}
OutParser& operator << (const char* ch) {
while (*ch) {
write_ch(*ch);
++ch;
}
return *this;
}
};
InParser fin("zapada.in");
OutParser fout("zapada.out");
struct vrajeala
{
int x, y, c;
bool operator < (const vrajeala A) const
{
return c < A.c;
}
}a[200005];
struct DSU
{
vector <int> t;
DSU(int n)
{
t.resize(n + 5);
}
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int r = x, y;
while (t[r])
r = t[r];
while (x != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
};
int n, m, q;
void Test_Case()
{
int i, nrcc, x, y, apm = 0;
fin >> n >> m >> q;
nrcc = n;
DSU tree(n);
for (i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
sort(a + 1, a + m + 1);
for (i = 1; i <= m and nrcc > 1; i++)
{
x = tree.Find(a[i].x);
y = tree.Find(a[i].y);
if (x != y)
{
tree.Union(x, y);
nrcc--;
apm += a[i].c;
}
}
fout << apm << "\n";
while (q--)
{
m++;
fin >> a[m].x >> a[m].y >> a[m].c;
if (a[m].c > a[m - 1].c)
{
m--;
fout << apm << "\n";
continue;
}
for (i = m; i >= 1 and a[i].c < a[i - 1].c; i--)
swap(a[i], a[i - 1]);
DSU tree(n);
apm = 0;
nrcc = n;
for (i = 1; i <= m and nrcc > 1; i++)
{
x = tree.Find(a[i].x);
y = tree.Find(a[i].y);
if (x != y)
{
tree.Union(x, y);
nrcc--;
apm += a[i].c;
}
}
fout << apm << "\n";
}
}
int main()
{
int t;
ios_base::sync_with_stdio(false);
cin.tie(0);
t = 1;
while (t--)
Test_Case();
return 0;
}