QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#34848#4251. Gamewe_wendys2 14ms39028kbC++146.8kb2022-06-12 09:32:252022-06-12 09:32:30

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-06-12 09:32:30]
  • 评测
  • 测评结果:2
  • 用时:14ms
  • 内存:39028kb
  • [2022-06-12 09:32:25]
  • 提交

answer

//https://qoj.ac/contest/948/problem/4251
//#pragma GCC optimize("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "game.h"
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;

template<class K> using sset =  tree<K, null_type, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll ceil0(ll a, ll b) {
    return a / b + ((a ^ b) > 0 && a % b);
}

int n, k;
int counter = 0;
const int mx = 5e5 + 5; //size limit
vector<int> adj[mx], scc_nodes[mx];
vector<int> comp_graph[mx];
int visited[mx], instack[mx], disc[mx], lowlink[mx], comp[mx];
set<int> comp_nodes;

int comp_visited[mx], comp_instack[mx], comp_disc[mx], comp_lowlink[mx], comp_comp[mx];
bool found = false;

//adj - adjacency list of the graph
//visitied - indicates if a node is visited or not
//instack - indicates if a node is currently in the stack or not
//disc - tells discovery time of that node
//lowlink - tells the lowlink value of that node
//comp - indicates which SCC the node belongs to
//comp_visited - indicates if a component is visited or not for comp tarjan
//comp_instack - indicates if a component is in the stack or not
//comp_disc - tell discovery time of that comp
//comp_lowlink - tells the lowlink value of the comp
//comp_comp - shows if the component has changed it's bigger component
//comp_graph - forest of inter comp edges
//comp_nodes - list of existing components

int yeah = 0, nah = 0; // count of yes and no

void tarjan(int u, stack<int> &st)
{
	disc[u] = lowlink[u] = ++counter;
	st.push(u);
	visited[u] = 1;
	instack[u] = 1;
	
	for(auto v : adj[u])
	{
		if(visited[v] == 0)
		{
			tarjan(v, st);
			lowlink[u] = min(lowlink[u], lowlink[v]);
		}
		else if(instack[v])
			lowlink[u] = min(lowlink[u], disc[v]); //This condition in only specific for articulation points
			// for bridges and SCC we can use lowlink[u]= min(lowlink[u], lowlink[v])
			// But here the definition of lowlink will obviously change
	}
	
	if(disc[u] == lowlink[u])
	{
		int node;
		do{
			node = st.top();
			st.pop();
			//cout<<node<<" ";
			instack[node] = 0;
			comp[node] = u;
		}while(node != u);
		//cout<<"\n";
	}
	
}

//To reset all values to perfrom tarjan again
void reset(int origin)
{
	for(int i : scc_nodes[origin])
	{
		visited[i] = 0;
		instack[i] = 0;
		disc[i] = 0;
		lowlink[i] = 0;
	}
	counter = 0;
}

vector<int> affected_comps; // to store all components that change after edge added b/w 2 different components

void super_join(int u, stack<int> &comp_st, int marker)
{
	comp_disc[u] = comp_lowlink[u] = ++counter;
	comp_st.push(u);
	comp_visited[u] = marker;
	comp_instack[u] = marker;
	comp_comp[u] = u;
	
	
	for(auto v : comp_graph[u])
	{
		if(comp_visited[comp[v]] != marker)
		{
			super_join(comp[v], comp_st, marker);
			comp_lowlink[u] = min(comp_lowlink[u], comp_lowlink[comp[v]]);
		}
		else if(comp_instack[comp[v]] == marker)
			comp_lowlink[u] = min(comp_lowlink[u], comp_disc[comp[v]]); //This condition in only specific for articulation points
			// for bridges and SCC we can use lowlink[u]= min(lowlink[u], lowlink[v])
			// But here the definition of lowlink will obviously change
	}
	
	if(comp_disc[u] == comp_lowlink[u])
	{
		int node;
		do{
			node = comp_st.top();
			comp_st.pop();
			//cout<<node<<" ";
			affected_comps.push_back(node);
			
			comp_instack[node] = 0;
			comp_comp[node] = u;
		}while(node != u);
		//cout<<"\n";
	}
}


int marker = 0;


void insert(int u, int v)
{
	adj[u].push_back(v); // add edge
	
	if(comp[u] == comp[v]) // if edge within component then do nothing
	{
		return;
	}
	else
	{
		comp_graph[comp[u]].push_back(v);

		counter = 0; // reset for new iteration
		
		stack<int> comp_st;
		affected_comps.clear();
		
		marker++;
		
		super_join(comp[u], comp_st, marker);
		
		//reconfiguring all data structures after any possible joins
		
		int origin_comp = comp_comp[comp[u]]; // all merged componenets belong here
		
		vector <int> temp_graph;	
		
		for(int i : affected_comps)
		{	
			if(comp_comp[i] != i && comp_comp[i] == origin_comp)
			{
				for (int vertex : scc_nodes[i])
				{
					comp[vertex] = origin_comp;
                    if(vertex < k) found = true;
					scc_nodes[origin_comp].push_back(vertex);
				}
				scc_nodes[i].clear();
			
			}
		}
		
		
		for (auto j : comp_graph[origin_comp])
		{
			if(origin_comp != comp_comp[comp[j]])
			{
				temp_graph.push_back(j);
			}
		}
		comp_graph[origin_comp].clear();
		
		for (auto j : temp_graph)
		{
			comp_graph[origin_comp].push_back(j);
		}
		temp_graph.clear();
		
		for(int i : affected_comps)
		{
			if(comp_comp[i] == origin_comp && i != origin_comp)
			{
				for (auto j : comp_graph[i])
				{
					if(comp_comp[i] != comp_comp[comp[j]])
					{
						comp_graph[origin_comp].push_back(j);
					}
				}
				comp_graph[i].clear();
				comp_nodes.erase(i);
			}
		}
		
		comp_nodes.insert(origin_comp);
	}
}

//Runs tarjan for only 1 component
void improved_tarjan(int u, int origin, stack<int> &st)
{
	disc[u] = lowlink[u] = ++counter;
	st.push(u);
	visited[u] = 1;
	instack[u] = 1;
	
	for(auto v : adj[u])
	{
		if(comp[v] == origin && visited[v] == 0)
		{
			improved_tarjan(v, origin, st);
			lowlink[u] = min(lowlink[u], lowlink[v]);
		}
		else if(instack[v])
			lowlink[u] = min(lowlink[u], disc[v]); //This condition in only specific for articulation points
			// for bridges and SCC we can use lowlink[u]= min(lowlink[u], lowlink[v])
			// But here the definition of lowlink will obviously change
	}
	
	if(disc[u] == lowlink[u])
	{
		int node;
		do{
			node = st.top();
			st.pop();
			//cout<<node<<" ";
			instack[node] = 0;
			comp[node] = u;
		}while(node != u);
		//cout<<"\n";
	}
	
}

void init(int n_, int k_){
    n = n_, k = k_;
    for(int i = 0; i < k - 1; i++) insert(i, i + 1);
    stack<int> st;
	
	// Run initial Tarjan
	for(int i = 0; i < n; i++)
	{
		if(!visited[i])
			tarjan(i, st);
	}
	
	// Add inter-component edges based on initial results
	for(int i = 0; i < n; i++)
	{
		comp_nodes.insert(comp[i]);
		
		scc_nodes[comp[i]].push_back(i);
		
		for(auto j : adj[i])
		{
			if(comp[j] != comp[i])
			{
				comp_graph[comp[i]].push_back(j);
			}
		}
	}
}

int add_teleporter(int u, int v){
    if(u == v) return u < k;
    insert(u, v);
    return found;
}

詳細信息

Subtask #1:

score: 2
Accepted

Test #1:

score: 2
Accepted
time: 7ms
memory: 38988kb

input:

1 1
1
893123 893123
-1

output:

0

result:

ok interaction finished.

Test #2:

score: 0
Accepted
time: 4ms
memory: 39008kb

input:

9 9
29
893122 893124
893121 893127
893120 893124
893123 893121
893122 893131
893125 893131
893121 893126
893123 893126
893126 893131
893123 893131
893123 893125
893123 893124
893127 893125
893120 893126
893123 893120
893121 893131
893123 893127
893122 893126
893122 893127
893127 893131
893122 893125...

output:

28

result:

ok interaction finished.

Test #3:

score: 0
Accepted
time: 14ms
memory: 38924kb

input:

100 100
80
893180 893071
893134 893063
893150 893091
893127 893178
893142 893177
893153 893156
893127 893137
893174 893065
893127 893070
893126 893061
893171 893089
893173 893072
893153 893058
893156 893074
893151 893068
893136 893060
893120 893083
893073 893091
893148 893163
893073 893088
893156 89...

output:

80
80
80
59

result:

ok interaction finished.

Test #4:

score: 0
Accepted
time: 11ms
memory: 38916kb

input:

45 45
80
893143 893167
893122 893132
893123 893140
893120 893139
893158 893167
893154 893163
893133 893137
893133 893142
893135 893137
893121 893135
893137 893149
893141 893152
893122 893167
893128 893145
893140 893167
893122 893127
893134 893142
893122 893129
893141 893156
893146 893149
893123 8931...

output:

80
49

result:

ok interaction finished.

Test #5:

score: 0
Accepted
time: 8ms
memory: 38892kb

input:

100 100
80
893169 893058
893132 893065
893143 893068
893153 893167
893152 893182
893138 893162
893129 893163
893146 893164
893134 893180
893142 893167
893144 893059
893132 893064
893135 893091
893164 893068
893123 893179
893126 893060
893136 893140
893179 893081
893139 893181
893120 893057
893172 89...

output:

80
80
80
42

result:

ok interaction finished.

Test #6:

score: 0
Accepted
time: 4ms
memory: 39020kb

input:

100 100
80
893135 893081
893170 893076
893148 893075
893134 893159
893159 893073
893170 893088
893131 893138
893121 893166
893171 893168
893127 893137
893147 893145
893062 893076
893160 893059
893063 893088
893137 893073
893123 893182
893152 893170
893141 893172
893137 893087
893167 893085
893147 89...

output:

80
80
80
37

result:

ok interaction finished.

Test #7:

score: 0
Accepted
time: 9ms
memory: 39028kb

input:

100 100
80
893062 893075
893139 893156
893137 893083
893071 893075
893072 893080
893141 893060
893126 893179
893064 893081
893167 893077
893139 893165
893056 893085
893169 893182
893062 893087
893141 893078
893062 893078
893129 893176
893065 893077
893141 893181
893152 893158
893151 893078
893157 89...

output:

80
80
80
59

result:

ok interaction finished.

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #8:

score: 10
Accepted
time: 4ms
memory: 38916kb

input:

100 10
80
893135 893150
893174 893168
893159 893149
893162 893082
893158 893129
893072 893150
893088 893079
893155 893154
893086 893126
893078 893153
893177 893138
893057 893066
893151 893089
893076 893162
893165 893164
893085 893170
893084 893128
893074 893083
893138 893148
893147 893167
893071 893...

output:

80
31

result:

ok interaction finished.

Test #9:

score: 0
Accepted
time: 7ms
memory: 38880kb

input:

100 10
80
893087 893068
893090 893073
893077 893169
893159 893156
893170 893062
893081 893145
893076 893083
893128 893078
893132 893139
893181 893165
893155 893167
893167 893089
893065 893081
893068 893180
893150 893175
893066 893183
893060 893133
893086 893060
893072 893142
893084 893132
893151 893...

output:

80
10

result:

ok interaction finished.

Test #10:

score: -10
Wrong Answer
time: 4ms
memory: 39020kb

input:

100 10
80
893136 893078
893085 893075
893173 893143
893132 893066
893066 893074
893149 893080
893152 893148
893179 893146
893174 893137
893082 893077
893140 893082
893080 893134
893171 893149
893070 893161
893087 893132
893168 893059
893086 893085
893159 893153
893143 893173
893167 893140
893062 893...

output:

80
26

result:

wrong answer Wrong Answer [1]

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%