제출 #526992

#제출 시각아이디문제언어결과실행 시간메모리
526992LucaDantas다리 (APIO19_bridges)C++17
0 / 100
2 ms588 KiB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 1e3+10, maxq = 1e4+10, B = maxn; // primeira subtask fds DEBUG <-------------------------------------->

struct DSU {
	int pai[maxn], peso[maxn];
	DSU() { for(int i = 0; i < maxn; i++) pai[i] = i, peso[i] = 1; }
	void init() { for(int i = 0; i < maxn; i++) pai[i] = i, peso[i] = 1; }
	int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
	void join(int a, int b) {
		a = find(a), b = find(b);
		if(a == b) return;
		if(peso[a] < peso[b]) swap(a, b);
		pai[b] = a;
		peso[a] += peso[b];
	}
	void reset(int a) { pai[a] = a, peso[a] = 1; }
	int sz(int a) { return peso[find(a)]; }
} dsu, dsu2;

struct Edge { int a, b, d, id; } e[maxn], certo[maxn];

struct Query { int tipo, x, y, t; } qr[maxq];

bool special[maxn], foi[maxn];

int ans[maxq];

int main() {
	memset(ans, -1, sizeof ans);
	
	int n, m; scanf("%d %d", &n, &m);
	for(int i = 0; i < m; i++)
		scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].d), e[i].id = i, certo[i] = e[i];

	int q; scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d %d %d", &qr[i].tipo, &qr[i].x, &qr[i].y);
		if(qr[i].tipo == 1) --qr[i].x;
		qr[i].t = i;
	}

	// ordenar as arestas pelo peso decrescente
	sort(e, e+n, [](const Edge& a, const Edge& b) { return a.d > b.d; });
	
	for(int k = 0; k < q; k += B) {
		vector<Query> ask;
		
		for(int i = k; i < min(q, k+B); i++) {
			if(qr[i].tipo == 1) // update
				special[qr[i].x] = 1 /*, printf("marquei %d\n", qr[i].x)*/ ;
			else ask.push_back(qr[i]);
		}

		sort(ask.begin(), ask.end(), [](const Query& a, const Query& b) { return a.y > b.y; }); // y==peso, ordeno pelo peso decrescente

		for(int i = 0, ptr = 0; i < (int)ask.size(); i++) {

			// printf("QUERY %d\n", ask[i].t);

			for(; ptr < m && e[ptr].d >= ask[i].y; ptr++)
				if(!special[e[ptr].id])
					dsu.join(e[ptr].a, e[ptr].b) /*, printf("juntei %d\n", e[ptr].id)*/ ;

			vector<int> ver = {ask[i].x}; // todos os caras que eu vou olhar no dsu2 pra ver se está junto ao vertice da pergunta

			// ERRADO
			for(int j = k; j < ask[i].t; j++) { // vejo todos os updates anteriores pra ver se adiciono ou não
				if(qr[j].tipo == 1 && qr[j].y >= ask[i].y) {
					int id = qr[j].x;
					dsu2.join(dsu.find(certo[id].a), dsu.find(certo[id].b));
					ver.push_back(dsu.find(certo[id].a)), ver.push_back(dsu.find(certo[id].b));
				}
			}

			// ERRADO
			for(int j = ask[i].t; j < min(q, k+B); j++) {
				if(qr[j].tipo == 1 && certo[qr[j].x].d >= ask[i].y) {
					int id = qr[j].x;
					dsu2.join(dsu.find(certo[id].a), dsu.find(certo[id].b));
					ver.push_back(dsu.find(certo[id].a)), ver.push_back(dsu.find(certo[id].b));
				}
			}

			/* puts("VER");
			for(int x : ver)
				printf("%d ", x);
			puts(""); */

			int val = 0;

			for(int x : ver) {
				if(!foi[x] && dsu2.find(ask[i].x) == dsu2.find(x))
					foi[x] = 1, val += dsu.sz(x);
			}

			// for(int x : ver) dsu2.reset(x), foi[x] = 0;
			dsu2.init(); memset(foi, 0, sizeof foi); // DEBUG <-------------------------------------->

			ans[ask[i].t] = val;

			// puts("");

		}


		// reiniciar as coisas pro próximo bloco
	}

	// puts("\nFIM\n");

	for(int i = 0; i < q; i++)
		if(ans[i] != -1) printf("%d\n", ans[i]);
}

컴파일 시 표준 에러 (stderr) 메시지

bridges.cpp: In function 'int main()':
bridges.cpp:33:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |  int n, m; scanf("%d %d", &n, &m);
      |            ~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |   scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].d), e[i].id = i, certo[i] = e[i];
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  int q; scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~
bridges.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |   scanf("%d %d %d", &qr[i].tipo, &qr[i].x, &qr[i].y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...