Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- map<string , set< string > > al;
- vector< string > q ,p;
- map<string , int> in;
- int n ,m ,d;
- vector< string > topoSort()
- {
- unordered_set< string > pq;
- for(auto i : al){
- pq.insert(i.first);
- for(auto j : i.second){
- pq.insert(j);
- }
- }
- for(auto i : al){
- for(auto j : i.second){
- in[j] += 1;
- pq.erase(j);
- }
- }
- vector< string > topo;
- while( !pq.empty() )
- {
- string u = *pq.begin();
- pq.erase(pq.begin());
- topo.push_back(u);
- for(auto v : al[u])
- {
- in[v] -= 1;
- if( in[v] == 0 ){
- pq.insert(v);
- }
- }
- }
- return topo;
- }
- int LIS()
- {
- map<string , int> idx;
- for(int i = 0 ; i < n ; i++) idx[ q[i] ] = i;
- set< int > lis;
- for(int i = 0 ; i < n ; i++)
- {
- if( idx.count( p[i] ) )
- {
- auto it = lis.upper_bound(idx[ p[i] ]);
- if( it != lis.end() ) lis.erase(it);
- lis.insert(idx[ p[i] ]);
- }
- }
- return lis.size();
- }
- int main()
- {
- cin >> n >> m >> d;
- for(int i = 0 ; i < m ; i++)
- {
- string u; cin >> u;
- string v; cin >> v;
- al[u].insert(v);
- }
- p.resize(n);
- for(auto &i : p) cin >> i;
- q = topoSort();
- cout << 2 * (n - LIS());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement