Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1
- 8
- ID4 Name4 Address4
- ID3 Name3 Address3
- ID2 Name2 Address2
- ID1 Name1 Address1
- ID5 Name5 Address5
- ID7 Name7 Address7
- ID6 Name6 Address6
- ID8 Name8 Address8
- ID4 2 ID3 ID7
- ID3 3 ID4 ID7 ID5
- ID2 1 ID8
- ID1 3 ID6 ID8 ID2
- ID5 2 ID3 ID7
- ID7 3 ID4 ID3 ID5
- ID6 1 ID1
- ID8 2 ID2 ID1
- 3
- ID2
- ID4
- ID3
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll long long
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll,ll>
- #define msi map<string,ll>
- #define mis map<ll, string>
- #define rep(i,a,b) for(ll i=a;i<b;i++)
- #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
- #define trav(a, x) for(auto& a : x)
- #define pii pair<ll,ll>
- #define vi vector<ll>
- #define vii vector<pair<ll, ll>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define max(a,b) (a>b?a:b)
- #define min(a,b) (a<b?a:b)
- /* For Debugging */
- #define DEBUG cerr<<"\n>>>I'm Here<<<\n"<<endl;
- #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
- #define what_is(x) cerr << #x << " is " << x << endl;
- std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
- #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
- #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
- #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
- #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace __gnu_pbds;
- using namespace std;
- #define PI 3.141592653589793
- #define N 100005
- /*
- Instructions :
- 1. Set p each i of p eqaul to i initailly.
- 2. rank is initiialised with 0.
- 3. To know parent run a loop for each value call find function.
- */
- ll high[N];
- ll p[N]; // set p of i equal to i initially
- ll find(ll x)
- {
- if(p[x]!=x)
- p[x]=find(p[x]);
- return p[x];
- }
- void merge(ll x,ll y)
- {
- ll Px,Py;
- Px=find(x);
- Py=find(y);
- if(high[Px]>high[Py])
- p[Py]=Px;
- else
- p[Px]=Py;
- if(high[Px]==high[Py])
- high[Py]++;
- }
- void solve()
- {
- ll n;
- cin>>n;
- string id,name,address;
- vector<vector<string>> v(n,vector<string>(3));
- map<string,ll> ma;
- rep(i,0,n)
- {
- cin>>v[i][0]>>v[i][1]>>v[i][2];
- ma[v[i][0]]=i;
- }
- rep(i,0,n)
- p[i]=i;
- string s;
- ll num1,k,num2;
- rep(i,0,n)
- {
- cin>>s;
- num1=ma[s];
- cin>>k;
- rep(i,0,k)
- {
- cin>>s;
- num2=ma[s];
- merge(num1,num2);
- }
- }
- vector<bool> vis(n,0);
- vector<string> out;
- cin>>k;
- rep(i,0,k)
- {
- cin>>s;
- vis[find(ma[s])]=1;
- }
- rep(i,0,n)
- {
- if(vis[find(i)])
- out.pb(v[i][0]);
- }
- sort(all(out));
- trav(i,out)
- {
- num1=ma[i];
- cout<<v[num1][0]<<" "<<v[num1][1]<<" "<<v[num1][2]<<endl;
- }
- return;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen ("input.txt","r",stdin);
- // freopen("output.txt","w",stdout);
- #endif
- FAST
- int TESTS=1;
- cin>>TESTS;
- while(TESTS--)
- {
- solve();
- }
- TIME
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment