feat: new graph problems formatted.
This commit is contained in:
62
metro/src/ac.cpp
Normal file
62
metro/src/ac.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vector<vector<int>> adj(n + 1);
|
||||
for (int i = 0; i < m; i++) {
|
||||
int u, v;
|
||||
cin >> u >> v;
|
||||
adj[u].push_back(v);
|
||||
adj[v].push_back(u);
|
||||
}
|
||||
|
||||
int central = -1;
|
||||
for (int v = 1; v <= n; v++) {
|
||||
if ((int)adj[v].size() >= 5) {
|
||||
central = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> degree(n + 1, 0);
|
||||
vector<char> removed(n + 1, false);
|
||||
queue<int> q;
|
||||
|
||||
removed[central] = true;
|
||||
for (int v = 1; v <= n; v++) {
|
||||
if (removed[v]) continue;
|
||||
for (int u : adj[v]) {
|
||||
if (u != central) degree[v]++;
|
||||
}
|
||||
if (degree[v] <= 1) q.push(v);
|
||||
}
|
||||
|
||||
while (!q.empty()) {
|
||||
int v = q.front();
|
||||
q.pop();
|
||||
|
||||
if (removed[v]) continue;
|
||||
removed[v] = true;
|
||||
|
||||
for (int u : adj[v]) {
|
||||
if (removed[u]) continue;
|
||||
degree[u]--;
|
||||
if (degree[u] == 1) q.push(u);
|
||||
}
|
||||
}
|
||||
|
||||
int answer = 0;
|
||||
for (int v = 1; v <= n; v++) {
|
||||
if (!removed[v]) answer++;
|
||||
}
|
||||
|
||||
cout << answer << '\n';
|
||||
return 0;
|
||||
}
|
||||
63
metro/src/alternative_ac.py
Normal file
63
metro/src/alternative_ac.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import sys
|
||||
from collections import deque
|
||||
|
||||
|
||||
def main():
|
||||
data = list(map(int, sys.stdin.buffer.read().split()))
|
||||
if not data:
|
||||
return
|
||||
|
||||
n, m = data[0], data[1]
|
||||
adj = [[] for _ in range(n + 1)]
|
||||
|
||||
idx = 2
|
||||
for _ in range(m):
|
||||
u, v = data[idx], data[idx + 1]
|
||||
idx += 2
|
||||
adj[u].append(v)
|
||||
adj[v].append(u)
|
||||
|
||||
central = -1
|
||||
for v in range(1, n + 1):
|
||||
if len(adj[v]) >= 5:
|
||||
central = v
|
||||
break
|
||||
|
||||
degree = [0] * (n + 1)
|
||||
removed = [False] * (n + 1)
|
||||
queue = deque()
|
||||
|
||||
removed[central] = True
|
||||
for v in range(1, n + 1):
|
||||
if removed[v]:
|
||||
continue
|
||||
for u in adj[v]:
|
||||
if u != central:
|
||||
degree[v] += 1
|
||||
if degree[v] <= 1:
|
||||
queue.append(v)
|
||||
|
||||
while queue:
|
||||
v = queue.popleft()
|
||||
|
||||
if removed[v]:
|
||||
continue
|
||||
removed[v] = True
|
||||
|
||||
for u in adj[v]:
|
||||
if removed[u]:
|
||||
continue
|
||||
degree[u] -= 1
|
||||
if degree[u] == 1:
|
||||
queue.append(u)
|
||||
|
||||
answer = 0
|
||||
for v in range(1, n + 1):
|
||||
if not removed[v]:
|
||||
answer += 1
|
||||
|
||||
print(answer)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
19
metro/src/checker.cpp
Normal file
19
metro/src/checker.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
setName("compare one integer");
|
||||
registerTestlibCmd(argc, argv);
|
||||
|
||||
int ja = ans.readInt();
|
||||
int pa = ouf.readInt();
|
||||
|
||||
if (ja != pa)
|
||||
quitf(_wa, "expected %d, found %d", ja, pa);
|
||||
|
||||
quitf(_ok, "answer is %d", ja);
|
||||
}
|
||||
83
metro/src/generator.cpp
Normal file
83
metro/src/generator.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_N = 0;
|
||||
const int MAX_N = 100;
|
||||
|
||||
const int rnd_test_n = 100;
|
||||
|
||||
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
|
||||
dest.insert(dest.end(), orig.begin(), orig.end());
|
||||
}
|
||||
|
||||
string output_tc(int x, int y) {
|
||||
ostringstream oss;
|
||||
oss << x << " " << y << endl;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(1, 1));
|
||||
tests.push_back(output_tc(2, 2));
|
||||
tests.push_back(output_tc(0, 0));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(100, 0));
|
||||
tests.push_back(output_tc(0, 100));
|
||||
return tests;
|
||||
}
|
||||
|
||||
string rnd_test(int i){
|
||||
int min_n = MIN_N;
|
||||
int max_n = MAX_N;
|
||||
|
||||
if(i<rnd_test_n / 3){
|
||||
max_n = 5;
|
||||
}
|
||||
else if(i<rnd_test_n / 2){
|
||||
max_n = 20;
|
||||
}
|
||||
|
||||
int x = rnd.next(min_n, max_n);
|
||||
int y = rnd.next(min_n, max_n);
|
||||
return(output_tc(x, y));
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
vector<string> tests;
|
||||
for (int i = 0; i < rnd_test_n; i++){
|
||||
tests.push_back(rnd_test(i));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
string extreme_test_1(){
|
||||
return(output_tc(100, 100));
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests(){
|
||||
vector<string> tests;
|
||||
tests.push_back(extreme_test_1());
|
||||
return tests;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
registerGen(argc, argv, 1);
|
||||
vector<string> tests;
|
||||
size_t test = 0;
|
||||
append(tests, generate_sample_tests());
|
||||
append(tests, generate_manual_tests());
|
||||
append(tests, generate_random_tests());
|
||||
append(tests, generate_extreme_tests());
|
||||
for (const auto &t : tests) {
|
||||
startTest(++test);
|
||||
cout << t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
7293
metro/src/jngen.h
Normal file
7293
metro/src/jngen.h
Normal file
File diff suppressed because it is too large
Load Diff
1
metro/src/script.sh
Normal file
1
metro/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
metro/src/testlib.h
Normal file
5963
metro/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
10
metro/src/validator.cpp
Normal file
10
metro/src/validator.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user