Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <cstdio>
- #include <cmath>
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <numeric>
- namespace Mlxa {
- using namespace std;
- typedef long long ll;
- #define read cin
- #define eol '\n'
- #define endln cout << eol
- template <class A> inline void print (A a) { cout << a << ' '; }
- template <class A> inline void println (A a) { cout << a << eol; }
- template <class A, class... B> inline void println (A a, B... b) { print(a); println(b...); }
- template <class A> inline void err (A a) { cerr << a << ' '; }
- template <class A> inline void errln (A a) { cerr << a << eol; }
- template <class A, class... B> inline void errln (A a, B... b) { cerr << a << ' '; errln(b...); }
- #ifdef AC
- #define addLog(...) errln("log:", __VA_ARGS__)
- #else
- #define addLog(...) (void(0))
- #endif
- #define pb push_back
- #define mp make_pair
- #define all(x) begin(x), end(x)
- #define openFiles(name) freopen(name ".in", "r", stdin), freopen(name ".out", "w", stdout)
- #define fastIO ios_base::sync_with_stdio(false), cin.tie(nullptr)
- } using namespace Mlxa;
- /*
- Getting edge graph for undirected unweighted graph
- */
- struct Graph
- {
- vector< set<size_t> > g;
- size_t N;
- Graph (size_t n) { assign(n); }
- void assign (size_t n)
- { N = n;
- g.assign(n + 1, set<size_t>());
- }
- void add (size_t i, size_t j)
- {
- g[i].insert(j);
- g[j].insert(i);
- }
- void refresh () { N = g.size(); }
- vector<size_t> edges (size_t v)
- { return vector<size_t>(all(g[v])); }
- void print ()
- {
- addLog("Logging graph:");
- for (size_t i = 0; i < N; ++ i) {
- err(i); err(":\t");
- for (size_t j : edges(i))
- err(j);
- errln("");
- }
- addLog("---------");
- }
- };
- struct EdgeId
- {
- size_t cur;
- map< pair<size_t, size_t>, size_t> m;
- EdgeId () : cur(0) {}
- void add (size_t i, size_t j)
- {
- if (i > j) swap(i, j);
- if (!m.count({i, j}))
- m[{i, j}] = cur ++;
- }
- size_t get (size_t i, size_t j)
- {
- if (i > j) swap(i, j);
- return m[{i, j}];
- }
- };
- /// O(E^2 (* log [by set]) )
- Graph edgeGraph (Graph &G)
- {
- EdgeId mapper;
- for (size_t i = 0; i < G.N; ++ i)
- for (size_t j : G.edges(i))
- mapper.add(i, j);
- Graph E(mapper.m.size());
- for (size_t i = 0; i < G.N; ++ i) {
- auto near = G.edges(i);
- addLog(i, near.size());
- for (size_t j : near)
- for (size_t k : near)
- if (j < k) {
- size_t ij = mapper.get(i, j);
- size_t ik = mapper.get(i, k);
- E.add(ij, ik);
- }
- }
- addLog("E.|V| =", E.N);
- return E;
- }
- int main () {
- addLog("Hello, world!");
- Graph G(2);
- G.g = {
- {1, 3},
- {0, 2},
- {1, 3},
- {0, 2}
- }; G.refresh();
- addLog(G.N);
- Graph E = edgeGraph(G);
- E.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement