Submission #375327

#TimeUsernameProblemLanguageResultExecution timeMemory
375327mode149256Monthly railway pass (LMIO18_menesinis_bilietas)C++17
100 / 100
564 ms116848 KiB
/*input 5 5 3 1 A 3 5 A 4 5 T 2 3 T 2 1 A */ #include <bits/stdc++.h> using namespace std; namespace my_template { typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<vl> vll; typedef vector<pi> vpi; typedef vector<vpi> vpii; typedef vector<pl> vpl; typedef vector<cd> vcd; typedef vector<pd> vpd; typedef vector<bool> vb; typedef vector<vb> vbb; typedef std::string str; typedef std::vector<str> vs; #define x first #define y second #define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n" const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L; template<typename T> pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); } template<typename T> pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); } template<typename T> T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); } template<typename T> T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); } template<typename T> void print(vector<T> vec, string name = "") { cout << name; for (auto u : vec) cout << u << ' '; cout << '\n'; } } using namespace my_template; const int MOD = 1000000007; const ll INF = std::numeric_limits<ll>::max(); const int MX = 500101; vii trauk(MX); vpi autob; vector<unordered_set<int>> newEdges(MX); vi sz; vi p; int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); } bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } void unionSet(int i, int j) { if (isSameSet(i, j)) return; int x = findSet(i), y = findSet(j); if (sz[x] < sz[y]) swap(x, y); p[y] = x; sz[x] += sz[y]; } vi visited; void dfs(int x) { visited[x] = true; for (auto u : trauk[x]) { if (!visited[u]) { dfs(u); unionSet(x, u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef LOCAL // freopen("lmio_2018_3e2_menesinis_bilietas_vyr.in", "r", stdin); // freopen("lmio_2018_3e2_menesinis_bilietas_vyr.out", "w", stdout); // #endif int N, M; cin >> N >> M; p = vi(N + 1); for (int i = 0; i <= N; i++) p[i] = i; sz = vi(N + 1, 1); for (int i = 0; i < M; ++i) { int a, b; char ch; cin >> a >> b >> ch; a--; b--; if (ch == 'A') { autob.emplace_back(a, b); } else { trauk[a].emplace_back(b); trauk[b].emplace_back(a); } } visited = vi(MX, 0); for (int i = 0; i < N; ++i) { if (!visited[i]) dfs(i); } for (auto u : autob) { if (isSameSet(u.x, u.y)) continue; newEdges[findSet(u.x)].insert(findSet(u.y)); newEdges[findSet(u.y)].insert(findSet(u.x)); } vi allNewNodes; { set<int> turiu; for (int i = 0; i < N; ++i) { turiu.insert(findSet(i)); } allNewNodes = vi(turiu.begin(), turiu.end()); } int newNodeCnt = (int)allNewNodes.size(); int ats = 0; for (auto u : allNewNodes) { // printf("u = %d, newEdges = %d, newNodeCnt = %d\n", u, (int)newEdges[u].size(), newNodeCnt); if (1 + (int)newEdges[u].size() == newNodeCnt) ats += sz[u]; } printf("%d\n", ats); } /* Look for: * special cases (n=1?) * overflow (ll vs int?) * the exact constraints (multiple sets are too slow for n=10^6 :( ) * array bounds */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...