sabertooth09

DCP-150 TLE

Jan 17th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. /*
  2. ID:sabertooth9
  3. TASK:DCP-150
  4. LANG: C++14
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define ll              long long
  9. #define pb              push_back
  10. #define read()         freopen("input.txt","r",stdin)
  11. #define write()        freopen("output.txt","w",stdout)
  12. #define lead_zero(x)    __builtin_clzll(x)
  13. #define trail_zero(x)   __builtin_ctz(x)
  14. #define total_1s(x)     __builtin_popcount(x)
  15. #define mod             100000007
  16. #define all(v)          v.begin(),v.end()
  17.  
  18. const ll N=100000;
  19.  
  20. vector< pair<ll,ll> >v[N+9];
  21. map<ll,pair<ll,ll>>mp;
  22. //itr.first==child itr.second.first==parent itr.second.second==weight
  23.  
  24. void create()
  25. {
  26.     map<ll,pair<ll,ll>>::iterator itr;
  27.     for(itr=mp.begin(); itr!=mp.end(); itr++)
  28.     {
  29.         v[itr->second.first].push_back(make_pair(itr->first,itr->second.second));
  30.     }
  31. }
  32.  
  33. /*void show()
  34. {
  35.     for(ll i=0; i<=N; i++)
  36.         if(v[i].size())
  37.         {
  38.             cout<<"showing "<<i<<":"<<endl;
  39.             for(ll j=0; j<v[i].size(); i++)
  40.             {
  41.                 cout<<v[i][j].first<<" "<<v[i][j].second<<endl;
  42.             }
  43.         }
  44. }
  45.  
  46. void show2()
  47. {
  48.     map<ll,pair<ll,ll>>::iterator itr;
  49.     for(itr=mp.begin(); itr!=mp.end(); itr++)
  50.     {
  51.         cout<<"source: "<<itr->second.first<<" Child :"<<itr->first<<" Weight :"<<itr->second.second<<endl;
  52.     }
  53. }*/
  54.  
  55. ll solve(ll source,ll en)
  56. {
  57.     priority_queue<ll>pq;
  58.     vector<ll>dis(N+9,mod);
  59.     dis[source]=0;
  60.     pq.push(source);
  61.     while(!pq.empty())
  62.     {
  63.         ll t1=pq.top();
  64.         pq.pop();
  65.         for(ll i=0; i<v[t1].size(); i++)
  66.         {
  67.             if(dis[t1]+v[t1][i].second<dis[v[t1][i].first])
  68.             {
  69.                 dis[v[t1][i].first]=dis[t1]+v[t1][i].second;
  70.                 pq.push(v[t1][i].first);
  71.             }
  72.         }
  73.     }
  74.     return dis[en];
  75. }
  76.  
  77. void init(ll n)
  78. {
  79.     for(ll i=0;i<n;i++)
  80.     {
  81.         if(mp[i].first==0)
  82.             mp[i].first=i;
  83.     }
  84. }
  85.  
  86. int main()
  87. {
  88.     read();
  89.     write();
  90.     ll n;
  91.     scanf("%lld",&n);
  92.     ll i,a,b;
  93.     for(i=1; i<n; i++)
  94.     {
  95.         scanf("%lld",&a);
  96.         //v[a].pb(make_pair(i+1,0));
  97.         mp[i+1]=make_pair(a,0);
  98.     }
  99.     for(i=1; i<n; i++)
  100.     {
  101.         scanf("%lld",&a);
  102.         mp[i+1].second=a;
  103.     }
  104.     //printf("%lld\n\n",mp[1].first);
  105.     create();
  106.     init(n);
  107.     //show2();
  108.     ll q;
  109.     scanf("%lld",&q);
  110.     while(q--)
  111.     {
  112.         scanf("%lld %lld",&a,&b);
  113.         if(a==1)
  114.         {
  115.             mp[b].first=b;
  116.             mp[b].second=0;
  117.             create();
  118.             init(n);
  119.         }
  120.         else
  121.         {
  122.             ll t=mp[b].first,x;
  123.             while(1)
  124.             {
  125.                 t=mp[t].first;
  126.                 if(t==mp[t].first)
  127.                 {
  128.                     break;
  129.                 }
  130.             }
  131.             printf("%lld %lld\n",t,solve(t,b));
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment