Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf6e6b0c86
|
||
|
|
9fd8ea1918
|
||
|
|
f55fb12e50
|
177
2023204/main.cpp
177
2023204/main.cpp
@@ -1,57 +1,126 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <vector>
|
|
||||||
#define DEAD false
|
#define DEAD false
|
||||||
#define ALIVE true
|
#define ALIVE true
|
||||||
// 'Natural count' is the name of the node when all the nodes in the tree is
|
// 'Natural count' is the name of the node when all the nodes in the tree is
|
||||||
// alive. xxx_num denotes the count as the question described.
|
// alive. xxx_num denotes the count as the question described.
|
||||||
|
|
||||||
|
// SEE ALSO BRANCH 04_02 WITH A CORRECT IMPLEMENTATION
|
||||||
|
|
||||||
int ans[200] = {0};
|
int ans[200] = {0};
|
||||||
long long dead_nodes_num[105] = {0};
|
long long dead_nodes_num[105] = {0};
|
||||||
int total_dead_nodes = 0;
|
int total_dead_nodes = 0;
|
||||||
long long target_nodes_num[105] = {0};
|
long long target_nodes_num[105] = {0};
|
||||||
int total_target_nodes = 0;
|
int total_target_nodes = 0;
|
||||||
long long max_spawned_num = 0;
|
|
||||||
|
|
||||||
struct Layer {
|
struct SeveralContinuedDeadNode {
|
||||||
long long start_num;
|
bool status;
|
||||||
long long end_num;
|
long long length; // Caution that when status == dead and length == 1, the node itself is alive.
|
||||||
std::vector<long long> dead_nodes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Layer tree[150] = {0};
|
struct SearchResult {
|
||||||
|
int next_ans_pos; // pos in the ans[] array.
|
||||||
|
long long count_in_father_line; // natural count.
|
||||||
|
};
|
||||||
|
|
||||||
void create_tree(long long start_num, long long prev_alive, int layer, int next_dead_node_ptr) {
|
SearchResult find_node(long long target, long long start_num, int next_dead_node_num_ptr,
|
||||||
tree[layer].start_num = start_num;
|
SeveralContinuedDeadNode *upper_line, long long upper_line_segment_count,
|
||||||
prev_alive *= 2;
|
int layer) {
|
||||||
tree[layer].end_num = start_num + prev_alive - 1;
|
long long spawned_node_max_num = start_num - 1;
|
||||||
while (next_dead_node_ptr < total_dead_nodes &&
|
long long segment_count = upper_line_segment_count;
|
||||||
dead_nodes_num[next_dead_node_ptr] <= tree[layer].end_num) {
|
SeveralContinuedDeadNode current_line_node[200] = {0};
|
||||||
tree[layer].dead_nodes.push_back(dead_nodes_num[next_dead_node_ptr++]);
|
long long current_line_len = 0;
|
||||||
prev_alive--;
|
bool all_dead = true;
|
||||||
}
|
|
||||||
if (prev_alive == 0 || tree[layer].end_num >= target_nodes_num[total_target_nodes - 1]) {
|
|
||||||
max_spawned_num = tree[layer].end_num;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
create_tree(tree[layer].end_num + 1, prev_alive, layer + 1, next_dead_node_ptr);
|
// Spawing all the nodes in current line
|
||||||
}
|
for (int upper_line_iter = 0; upper_line_iter < upper_line_segment_count; upper_line_iter++) {
|
||||||
|
if (upper_line[upper_line_iter].status == DEAD) {
|
||||||
|
current_line_node[current_line_len] = upper_line[upper_line_iter];
|
||||||
|
current_line_node[current_line_len].length *= 2;
|
||||||
|
current_line_len++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
all_dead = false;
|
||||||
|
long long total_spawned_after =
|
||||||
|
spawned_node_max_num + upper_line[upper_line_iter].length * 2;
|
||||||
|
while (dead_nodes_num[next_dead_node_num_ptr] <= total_spawned_after &&
|
||||||
|
next_dead_node_num_ptr < total_dead_nodes) {
|
||||||
|
if (dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num > 1) {
|
||||||
|
current_line_node[current_line_len] = SeveralContinuedDeadNode{
|
||||||
|
ALIVE, dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num - 1};
|
||||||
|
current_line_len++; // things before this new dead node
|
||||||
|
}
|
||||||
|
current_line_node[current_line_len] =
|
||||||
|
SeveralContinuedDeadNode{DEAD, 1}; // this new dead node
|
||||||
|
current_line_len++;
|
||||||
|
|
||||||
void find_ans(long long target, int layer) {
|
spawned_node_max_num = dead_nodes_num[next_dead_node_num_ptr]; // we now spawned to
|
||||||
ans[layer] = target;
|
// this dead node.
|
||||||
if (layer == 1) {
|
next_dead_node_num_ptr++;
|
||||||
return;
|
}
|
||||||
}
|
if (spawned_node_max_num < total_spawned_after) {
|
||||||
long long count_in_layer = (target - tree[layer].start_num) / 2;
|
current_line_node[current_line_len] =
|
||||||
for (int i = 0; i < tree[layer - 1].dead_nodes.size(); i++) {
|
SeveralContinuedDeadNode{ALIVE, total_spawned_after - spawned_node_max_num};
|
||||||
if (count_in_layer < tree[layer - 1].dead_nodes[i] - tree[layer - 1].start_num - i) {
|
current_line_len++;
|
||||||
find_ans(tree[layer - 1].start_num + i + count_in_layer, layer - 1);
|
}
|
||||||
return;
|
|
||||||
|
spawned_node_max_num = total_spawned_after;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// til the end_num
|
|
||||||
find_ans(tree[layer - 1].start_num + tree[layer - 1].dead_nodes.size() + count_in_layer,
|
if (all_dead) {
|
||||||
layer - 1);
|
return SearchResult{1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target <= spawned_node_max_num) {
|
||||||
|
ans[0] = target;
|
||||||
|
long long natural_pos = -1, num = start_num - 1;
|
||||||
|
for (int i = 0; i < current_line_len; i++) {
|
||||||
|
if (current_line_node[i].status == DEAD) {
|
||||||
|
natural_pos += current_line_node[i].length;
|
||||||
|
num += (current_line_node[i].length == 1);
|
||||||
|
if (num == target) {
|
||||||
|
return SearchResult{1, natural_pos / 2};
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now deal with alive nodes
|
||||||
|
if (num + current_line_node[i].length < target) {
|
||||||
|
natural_pos += current_line_node[i].length;
|
||||||
|
num += current_line_node[i].length;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// if we made it here, then the target > num + current_line_node[i].length
|
||||||
|
return SearchResult{1, (natural_pos + target - num) / 2};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SearchResult{1, -1};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
SearchResult current_line_pos =
|
||||||
|
find_node(target, spawned_node_max_num + 1, next_dead_node_num_ptr, current_line_node,
|
||||||
|
current_line_len, layer + 1);
|
||||||
|
if (current_line_pos.count_in_father_line < 0) {
|
||||||
|
// The path does not exist.
|
||||||
|
return SearchResult{1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
long long natural_count = -1, num = start_num - 1;
|
||||||
|
for (int i = 0; i < current_line_len; i++) {
|
||||||
|
natural_count += current_line_node[i].length;
|
||||||
|
if (current_line_node[i].status == ALIVE || current_line_node[i].length == 1) {
|
||||||
|
num += current_line_node[i].length;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (natural_count >= current_line_pos.count_in_father_line) {
|
||||||
|
num -= natural_count - current_line_pos.count_in_father_line;
|
||||||
|
ans[current_line_pos.next_ans_pos++] = num;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SearchResult{current_line_pos.next_ans_pos,
|
||||||
|
current_line_pos.count_in_father_line / 2};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -62,13 +131,11 @@ int main() {
|
|||||||
for (int i = 0; i < total_target_nodes; i++) {
|
for (int i = 0; i < total_target_nodes; i++) {
|
||||||
scanf("%lld", &target_nodes_num[i]);
|
scanf("%lld", &target_nodes_num[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
tree[1].start_num = 1;
|
|
||||||
tree[1].end_num = 1;
|
|
||||||
if (dead_nodes_num[0] == 1) {
|
if (dead_nodes_num[0] == 1) {
|
||||||
for (int i = 0; i < total_target_nodes; i++) {
|
for (int i = 0; i < total_target_nodes; i++) {
|
||||||
if (target_nodes_num[i] == 1) {
|
if (target_nodes_num[i] == 1) {
|
||||||
printf("1\n"); //
|
printf("1\n");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("0\n");
|
printf("0\n");
|
||||||
@@ -76,25 +143,21 @@ int main() {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
create_tree(2, 1, 2, 0);
|
SeveralContinuedDeadNode firstline = SeveralContinuedDeadNode{ALIVE, 1};
|
||||||
|
for (int i = 0; i < total_target_nodes; i++) {
|
||||||
// printf("Max spawn: %lld\n", max_spawned_num);
|
SearchResult result = find_node(target_nodes_num[i], 2, 0, &firstline, 1, 2);
|
||||||
|
if (result.count_in_father_line == -1) {
|
||||||
for (int i = 0; i < total_target_nodes; i++) {
|
printf("0\n");
|
||||||
if (target_nodes_num[i] > max_spawned_num) {
|
}
|
||||||
printf("0\n");
|
else {
|
||||||
continue;
|
printf("1 ");
|
||||||
|
for (int j = result.next_ans_pos - 1; j >= 0; j--) {
|
||||||
|
printf("%lld ", ans[j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int layer_pos = 1;
|
|
||||||
while (tree[layer_pos].end_num < target_nodes_num[i]) {
|
|
||||||
layer_pos++;
|
|
||||||
}
|
|
||||||
find_ans(target_nodes_num[i], layer_pos);
|
|
||||||
for (int j = 1; j <= layer_pos; j++) {
|
|
||||||
printf("%lld ", ans[j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
71
2023205/main.cpp
Normal file
71
2023205/main.cpp
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
// 并查集。并查集只关注双向连通性。
|
||||||
|
|
||||||
|
struct Edge {
|
||||||
|
unsigned short end;
|
||||||
|
int next;
|
||||||
|
};
|
||||||
|
|
||||||
|
Edge edges[1050000];
|
||||||
|
char nodes_visited[65540];
|
||||||
|
int head[65540], len;
|
||||||
|
|
||||||
|
int add_edge(int start, int terminal) {
|
||||||
|
edges[++len].end = terminal;
|
||||||
|
edges[len].next = head[start];
|
||||||
|
head[start] = len;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int dfs(int start) {
|
||||||
|
if (nodes_visited[start] >= 1) {
|
||||||
|
nodes_visited[start] = 2;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
nodes_visited[start] = 1;
|
||||||
|
for (int j = head[start]; j != 0; j = edges[j].next) {
|
||||||
|
dfs(edges[j].end);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int N, M;
|
||||||
|
scanf("%d %d", &N, &M);
|
||||||
|
for (int start = 0; start < N; start++) {
|
||||||
|
int edge_count;
|
||||||
|
scanf("%d", &edge_count);
|
||||||
|
for (int j = 0; j < edge_count; j++) {
|
||||||
|
int terminal;
|
||||||
|
scanf("%d", &terminal);
|
||||||
|
add_edge(start, terminal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs(0);
|
||||||
|
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
if (nodes_visited[i] < 1) {
|
||||||
|
// Not connected!
|
||||||
|
for (int j = 0; j <= M; j++) {
|
||||||
|
printf("0\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("1\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < M; i++) {
|
||||||
|
int start, end;
|
||||||
|
scanf("%d %d", &start, &end);
|
||||||
|
if (nodes_visited[end] == 2) {
|
||||||
|
printf("1\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("0\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user