Submission #930042

# Submission time Handle Problem Language Result Execution time Memory
930042 2024-02-18T09:48:03 Z pan Rigged Roads (NOI19_riggedroads) C++17
100 / 100
267 ms 171440 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%d", &x);
#define input2(x, y) scanf("%d%d", &x, &y);
#define input3(x, y, z) scanf("%d%d%d", &x, &y, &z);
#define input4(x, y, z, a) scanf("%d%d%d%d", &x, &y, &z, &a);
#define print(x, y) printf("%d%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef int ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
ll const MAXN = 3e5+5;
ll n, m, x;
vector<pi> edges;
bool mst[MAXN];
vector<pi> adj[MAXN];
vector<ll> cost;
// LCA
int lg(unsigned long long i) {
    return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
}
ll label;
ll k, maxr;
vector<vector<pi > >rmq;
ll fa[MAXN],depth[MAXN], par[MAXN], ppath[MAXN];
vector<pi> arr;

void dfs(ll p, ll node) // euler path
{
	label++;
	fa[node] = label;
	arr.pb(mp(depth[node], node));
	for (pi u: adj[node])
	{
		if (u.f==p) continue;
		par[u.f] = node;
		ppath[u.f] = u.s;
		depth[u.f] = depth[node]+1;
		dfs(node, u.f);
		label++;
		arr.pb(mp(depth[node], node));
	}

}

void initrmq()
{
	maxr = arr.size();
	k = lg(maxr);
	rmq.assign(k+5, vector<pi> (maxr));
	rmq[0] = arr;
	for (ll i=1; i<=k; ++i)
	{
		for (ll j=0; j+ (1<<i) < maxr; ++j)
		{
			if (rmq[i-1][j].f < rmq[i-1][j + (1 << (i - 1))].f)
			{
				rmq[i][j] = rmq[i-1][j];
			}
			else
			{
				rmq[i][j] = rmq[i-1][j + (1 << (i - 1))];
			}
			
		}
	}
}

ll lca(ll node1, ll node2)
{
	ll l = fa[node1], r = fa[node2];
	if (l>r) swap(l,r);
	ll i = lg(r-l+1);
	if (rmq[i][l] < rmq[i][r - (1 << i) + 1])
	{
		return rmq[i][l].s;
	}
	else
	{
		return rmq[i][r - (1 << i) + 1].s;
	}
}

// UFDS
vector<ll> link1;
ll find(ll x)
{
	if (link1[x]!=x) link1[x] = find(link1[x]);
	return link1[x];
}
void unite(ll x, ll y)
{
	x = find(x), y = find(y);
	if (depth[x]>depth[y]) swap(x,y);
	link1[y] = link1[x];
}
// general
vector<ll> tobeset;
void update(ll from, ll until)
{
	from = find(from), until = find(until);
	ll counter = 0;
	while (from!=until)
	{
		counter++;
		tobeset.pb(ppath[from]);
		unite(from, par[from]);
		from = find(from), until = find(until);
	}
}
int main()
{
	input2(n, m);
	link1.resize(n);
	edges.resize(m);
	cost.assign(m, -1);
	for (ll i=0; i<m; ++i) {input2(edges[i].f, edges[i].s); mst[i] = false; edges[i].f--; edges[i].s--;}
	for (ll i=0; i<n-1; ++i) 
	{
	    input(x); 
		x--;
		mst[x] = true;
		adj[edges[x].f].pb(mp(edges[x].s, x));
		adj[edges[x].s].pb(mp(edges[x].f, x));
	}
	label = -1;
	depth[0] = 0;
	dfs(-1, 0);
	initrmq();
	for (ll i=0; i<n; ++i) link1[i] = i;
	label = 0;
	for (ll i=0; i<m; ++i)
	{
		if (cost[i]!=-1) continue;
		if (mst[i]) {cost[i] = ++label; unite(edges[i].f, edges[i].s); continue;}
		ll lcanow = lca(edges[i].f, edges[i].s);
		tobeset.resize(0);
		update(edges[i].f, lcanow);
		update(edges[i].s, lcanow);
		sort(tobeset.begin(), tobeset.end());
		for (ll j=0; j<tobeset.size(); ++j) {cost[tobeset[j]] = ++label;}
		cost[i] = ++label;
	}
	for (ll i=0; i<m; ++i) print(cost[i], ' ');
	
	
	
	return 0;
}

Compilation message

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:159:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |   for (ll j=0; j<tobeset.size(); ++j) {cost[tobeset[j]] = ++label;}
      |                ~^~~~~~~~~~~~~~~
riggedroads.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%d%d", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~
riggedroads.cpp:131:2: note: in expansion of macro 'input2'
  131 |  input2(n, m);
      |  ^~~~~~
riggedroads.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%d%d", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~
riggedroads.cpp:135:26: note: in expansion of macro 'input2'
  135 |  for (ll i=0; i<m; ++i) {input2(edges[i].f, edges[i].s); mst[i] = false; edges[i].f--; edges[i].s--;}
      |                          ^~~~~~
riggedroads.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%d", &x);
      |                  ~~~~~^~~~~~~~~~
riggedroads.cpp:138:6: note: in expansion of macro 'input'
  138 |      input(x);
      |      ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11864 KB Output is correct
2 Correct 3 ms 11864 KB Output is correct
3 Correct 3 ms 11864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11864 KB Output is correct
2 Correct 3 ms 11864 KB Output is correct
3 Correct 3 ms 11864 KB Output is correct
4 Correct 4 ms 12124 KB Output is correct
5 Correct 3 ms 12124 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Correct 3 ms 12124 KB Output is correct
8 Correct 3 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 54340 KB Output is correct
2 Correct 100 ms 67668 KB Output is correct
3 Correct 84 ms 25160 KB Output is correct
4 Correct 154 ms 142592 KB Output is correct
5 Correct 165 ms 151352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 66400 KB Output is correct
2 Correct 53 ms 31144 KB Output is correct
3 Correct 28 ms 21724 KB Output is correct
4 Correct 70 ms 52128 KB Output is correct
5 Correct 26 ms 27856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 131344 KB Output is correct
2 Correct 218 ms 150052 KB Output is correct
3 Correct 56 ms 50108 KB Output is correct
4 Correct 88 ms 69260 KB Output is correct
5 Correct 262 ms 171440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 92608 KB Output is correct
2 Correct 84 ms 65984 KB Output is correct
3 Correct 254 ms 152244 KB Output is correct
4 Correct 223 ms 137664 KB Output is correct
5 Correct 21 ms 23260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11864 KB Output is correct
2 Correct 3 ms 11864 KB Output is correct
3 Correct 3 ms 11864 KB Output is correct
4 Correct 4 ms 12124 KB Output is correct
5 Correct 3 ms 12124 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Correct 3 ms 12124 KB Output is correct
8 Correct 3 ms 12124 KB Output is correct
9 Correct 60 ms 54340 KB Output is correct
10 Correct 100 ms 67668 KB Output is correct
11 Correct 84 ms 25160 KB Output is correct
12 Correct 154 ms 142592 KB Output is correct
13 Correct 165 ms 151352 KB Output is correct
14 Correct 95 ms 66400 KB Output is correct
15 Correct 53 ms 31144 KB Output is correct
16 Correct 28 ms 21724 KB Output is correct
17 Correct 70 ms 52128 KB Output is correct
18 Correct 26 ms 27856 KB Output is correct
19 Correct 227 ms 131344 KB Output is correct
20 Correct 218 ms 150052 KB Output is correct
21 Correct 56 ms 50108 KB Output is correct
22 Correct 88 ms 69260 KB Output is correct
23 Correct 262 ms 171440 KB Output is correct
24 Correct 139 ms 92608 KB Output is correct
25 Correct 84 ms 65984 KB Output is correct
26 Correct 254 ms 152244 KB Output is correct
27 Correct 223 ms 137664 KB Output is correct
28 Correct 21 ms 23260 KB Output is correct
29 Correct 247 ms 127940 KB Output is correct
30 Correct 267 ms 137920 KB Output is correct
31 Correct 256 ms 145728 KB Output is correct
32 Correct 79 ms 27644 KB Output is correct
33 Correct 261 ms 146024 KB Output is correct