Submission #681178

# Submission time Handle Problem Language Result Execution time Memory
681178 2023-01-12T13:38:34 Z arnold518 Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
4323 ms 106348 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 500;
const int MAXM = 1e5;
const int MAXQ = 1e6;
const ll INF = 1e18;

struct Edge
{
	int u, v; ll w;
};

int N, M, Q;
Edge A[MAXM+10];
int B[MAXQ+10];

struct UF
{
	int par[MAXN+10], sz[MAXN+10];
	void init()
	{
		for(int i=1; i<=N; i++) par[i]=i, sz[i]=1;
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x); y=Find(y);
		if(x==y) return;
		if(sz[x]>sz[y]) swap(x, y);
		if(sz[x]==sz[y]) sz[y]++;
		par[x]=y;
	}
};

ll ans[MAXQ+10];
int KL[MAXM+10], KR[MAXM+10];
map<ll, pll> MM;

pll operator + (const pll &p, const pll &q) { return pll(p.first+q.first, p.second+q.second); }

int main()
{
	scanf("%d%d", &N, &M);
	for(int i=1; i<=M; i++)
	{
		int u, v, w;
		scanf("%d%d%lld", &u, &v, &w); w*=2;
		A[i]={u, v, w};
	}
	sort(A+1, A+M+1, [&](const Edge &p, const Edge &q) { return p.w<q.w; });
	scanf("%d", &Q);
	for(int i=1; i<=Q; i++) scanf("%d", &B[i]), B[i]*=2;
	for(int i=1; i<=Q; i++) MM[B[i]];
	A[0].w=-INF, A[M+1].w=INF;

	for(int i=1; i<=M; i++) KR[i]=M+1, KL[i]=0;

	UF uf;
	vector<int> V;
	for(int i=M; i>=1; i--)
	{
		uf.init();
		for(auto it : V)
		{
			uf.Union(A[it].u, A[it].v);
			if(uf.Find(A[i].u)==uf.Find(A[i].v))
			{
				KR[i]=it;
				break;
			}
		}
		uf.init();
		uf.Union(A[i].u, A[i].v);
		vector<int> VV;
		VV.push_back(i);
		for(auto it : V)
		{
			if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
			uf.Union(A[it].u, A[it].v);
			VV.push_back(it);
		}
		V=VV;
	}

	V.clear();
	for(int i=1; i<=M; i++)
	{
		uf.init();
		for(auto it : V)
		{
			uf.Union(A[it].u, A[it].v);
			if(uf.Find(A[i].u)==uf.Find(A[i].v))
			{
				KL[i]=it;
				break;
			}
		}
		uf.init();
		uf.Union(A[i].u, A[i].v);
		vector<int> VV;
		VV.push_back(i);
		for(auto it : V)
		{
			if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
			uf.Union(A[it].u, A[it].v);
			VV.push_back(it);
		}
		V=VV;
	}

	for(int i=1; i<=M; i++)
	{
		ll l=A[KL[i]].w+A[i].w>>1;
		ll r=A[KR[i]].w+A[i].w>>1;
		//printf("%lld %lld %lld\n", l, A[i].w, r);

		MM[l]=MM[l]+pll(-1, A[i].w);
		MM[A[i].w]=MM[A[i].w]+pll(2, -2*A[i].w);
		MM[r]=MM[r]+pll(-1, A[i].w);
	}

	for(auto it=next(MM.begin()); it!=MM.end(); it++) it->second=it->second+prev(it)->second;

	for(int i=1; i<=Q; i++)
	{
		pll p=MM[B[i]];
		printf("%lld\n", (p.first*B[i]+p.second)/2);
	}
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:56:17: warning: format '%lld' expects argument of type 'long long int*', but argument 4 has type 'int*' [-Wformat=]
   56 |   scanf("%d%d%lld", &u, &v, &w); w*=2;
      |              ~~~^           ~~
      |                 |           |
      |                 |           int*
      |                 long long int*
      |              %d
reconstruction.cpp:122:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  122 |   ll l=A[KL[i]].w+A[i].w>>1;
      |        ~~~~~~~~~~^~~~~~~
reconstruction.cpp:123:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |   ll r=A[KR[i]].w+A[i].w>>1;
      |        ~~~~~~~~~~^~~~~~~
reconstruction.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
reconstruction.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |   scanf("%d%d%lld", &u, &v, &w); w*=2;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
reconstruction.cpp:61:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  for(int i=1; i<=Q; i++) scanf("%d", &B[i]), B[i]*=2;
      |                          ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 312 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 312 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3543 ms 16160 KB Output is correct
21 Correct 2220 ms 16040 KB Output is correct
22 Correct 2367 ms 16040 KB Output is correct
23 Correct 2518 ms 16044 KB Output is correct
24 Correct 3047 ms 16048 KB Output is correct
25 Correct 3602 ms 16056 KB Output is correct
26 Correct 3675 ms 16052 KB Output is correct
27 Correct 3495 ms 15280 KB Output is correct
28 Correct 3487 ms 3644 KB Output is correct
29 Correct 3459 ms 3504 KB Output is correct
30 Correct 3485 ms 16040 KB Output is correct
31 Correct 3448 ms 15952 KB Output is correct
32 Correct 3443 ms 15788 KB Output is correct
33 Correct 3437 ms 15796 KB Output is correct
34 Correct 3378 ms 3276 KB Output is correct
35 Correct 3472 ms 15792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 3026 ms 85164 KB Output is correct
5 Correct 3020 ms 95932 KB Output is correct
6 Correct 3007 ms 95860 KB Output is correct
7 Correct 1899 ms 97728 KB Output is correct
8 Correct 1758 ms 97872 KB Output is correct
9 Correct 1708 ms 97928 KB Output is correct
10 Correct 3060 ms 104356 KB Output is correct
11 Correct 1733 ms 106348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 312 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 675 ms 80168 KB Output is correct
21 Correct 680 ms 89092 KB Output is correct
22 Correct 675 ms 88924 KB Output is correct
23 Correct 655 ms 89148 KB Output is correct
24 Correct 682 ms 88984 KB Output is correct
25 Correct 655 ms 89040 KB Output is correct
26 Correct 674 ms 88912 KB Output is correct
27 Correct 609 ms 88976 KB Output is correct
28 Correct 636 ms 89156 KB Output is correct
29 Correct 673 ms 89060 KB Output is correct
30 Correct 652 ms 89164 KB Output is correct
31 Correct 672 ms 88916 KB Output is correct
32 Correct 614 ms 89452 KB Output is correct
33 Correct 630 ms 89064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 312 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3543 ms 16160 KB Output is correct
21 Correct 2220 ms 16040 KB Output is correct
22 Correct 2367 ms 16040 KB Output is correct
23 Correct 2518 ms 16044 KB Output is correct
24 Correct 3047 ms 16048 KB Output is correct
25 Correct 3602 ms 16056 KB Output is correct
26 Correct 3675 ms 16052 KB Output is correct
27 Correct 3495 ms 15280 KB Output is correct
28 Correct 3487 ms 3644 KB Output is correct
29 Correct 3459 ms 3504 KB Output is correct
30 Correct 3485 ms 16040 KB Output is correct
31 Correct 3448 ms 15952 KB Output is correct
32 Correct 3443 ms 15788 KB Output is correct
33 Correct 3437 ms 15796 KB Output is correct
34 Correct 3378 ms 3276 KB Output is correct
35 Correct 3472 ms 15792 KB Output is correct
36 Correct 3629 ms 18216 KB Output is correct
37 Correct 2322 ms 18440 KB Output is correct
38 Correct 2284 ms 18496 KB Output is correct
39 Correct 2457 ms 18484 KB Output is correct
40 Correct 2907 ms 18460 KB Output is correct
41 Correct 3392 ms 18416 KB Output is correct
42 Correct 3451 ms 18376 KB Output is correct
43 Correct 3459 ms 17732 KB Output is correct
44 Correct 3441 ms 6108 KB Output is correct
45 Correct 3404 ms 5988 KB Output is correct
46 Correct 3472 ms 18548 KB Output is correct
47 Correct 3476 ms 18636 KB Output is correct
48 Correct 3473 ms 18544 KB Output is correct
49 Correct 3507 ms 18560 KB Output is correct
50 Correct 3431 ms 6136 KB Output is correct
51 Correct 3484 ms 18668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 312 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3543 ms 16160 KB Output is correct
21 Correct 2220 ms 16040 KB Output is correct
22 Correct 2367 ms 16040 KB Output is correct
23 Correct 2518 ms 16044 KB Output is correct
24 Correct 3047 ms 16048 KB Output is correct
25 Correct 3602 ms 16056 KB Output is correct
26 Correct 3675 ms 16052 KB Output is correct
27 Correct 3495 ms 15280 KB Output is correct
28 Correct 3487 ms 3644 KB Output is correct
29 Correct 3459 ms 3504 KB Output is correct
30 Correct 3485 ms 16040 KB Output is correct
31 Correct 3448 ms 15952 KB Output is correct
32 Correct 3443 ms 15788 KB Output is correct
33 Correct 3437 ms 15796 KB Output is correct
34 Correct 3378 ms 3276 KB Output is correct
35 Correct 3472 ms 15792 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 3026 ms 85164 KB Output is correct
40 Correct 3020 ms 95932 KB Output is correct
41 Correct 3007 ms 95860 KB Output is correct
42 Correct 1899 ms 97728 KB Output is correct
43 Correct 1758 ms 97872 KB Output is correct
44 Correct 1708 ms 97928 KB Output is correct
45 Correct 3060 ms 104356 KB Output is correct
46 Correct 1733 ms 106348 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 675 ms 80168 KB Output is correct
49 Correct 680 ms 89092 KB Output is correct
50 Correct 675 ms 88924 KB Output is correct
51 Correct 655 ms 89148 KB Output is correct
52 Correct 682 ms 88984 KB Output is correct
53 Correct 655 ms 89040 KB Output is correct
54 Correct 674 ms 88912 KB Output is correct
55 Correct 609 ms 88976 KB Output is correct
56 Correct 636 ms 89156 KB Output is correct
57 Correct 673 ms 89060 KB Output is correct
58 Correct 652 ms 89164 KB Output is correct
59 Correct 672 ms 88916 KB Output is correct
60 Correct 614 ms 89452 KB Output is correct
61 Correct 630 ms 89064 KB Output is correct
62 Correct 3629 ms 18216 KB Output is correct
63 Correct 2322 ms 18440 KB Output is correct
64 Correct 2284 ms 18496 KB Output is correct
65 Correct 2457 ms 18484 KB Output is correct
66 Correct 2907 ms 18460 KB Output is correct
67 Correct 3392 ms 18416 KB Output is correct
68 Correct 3451 ms 18376 KB Output is correct
69 Correct 3459 ms 17732 KB Output is correct
70 Correct 3441 ms 6108 KB Output is correct
71 Correct 3404 ms 5988 KB Output is correct
72 Correct 3472 ms 18548 KB Output is correct
73 Correct 3476 ms 18636 KB Output is correct
74 Correct 3473 ms 18544 KB Output is correct
75 Correct 3507 ms 18560 KB Output is correct
76 Correct 3431 ms 6136 KB Output is correct
77 Correct 3484 ms 18668 KB Output is correct
78 Correct 4111 ms 95388 KB Output is correct
79 Correct 2794 ms 97228 KB Output is correct
80 Correct 2945 ms 96556 KB Output is correct
81 Correct 3144 ms 96260 KB Output is correct
82 Correct 3627 ms 95356 KB Output is correct
83 Correct 4101 ms 95228 KB Output is correct
84 Correct 4134 ms 95228 KB Output is correct
85 Correct 4135 ms 94864 KB Output is correct
86 Correct 4058 ms 90712 KB Output is correct
87 Correct 4067 ms 92344 KB Output is correct
88 Correct 4091 ms 103236 KB Output is correct
89 Correct 4153 ms 103240 KB Output is correct
90 Correct 4121 ms 103344 KB Output is correct
91 Correct 4284 ms 103064 KB Output is correct
92 Correct 4260 ms 93544 KB Output is correct
93 Correct 4323 ms 104296 KB Output is correct