Comments

ya , but that's not the main point, the point is about my question

ok sorry for that , i am knew so dont know much about how to use that .

sorry i didnt get you ??

could someone tell me whether i am wrong with these i have tried my own these problem link

void solve(vector<int>v[],int src,vector<int>&euler,unordered_map<int,int>&mp,int &counter) {
    euler.pb(src);
    if(mp.find(src) == mp.end()) mp[src] = counter;
    for(auto i:v[src]) {
        counter++;

        solve(v,i,euler,mp,counter);
        counter++;
        euler.push_back(src);
    }
}

void storeminimum(vector<int>&euler,int sq,vector<int>&sqrtd) {
    for(int i=0;i<euler.size();i++) {
        sqrtd[i/sq] = min(sqrtd[i/sq],euler[i]);
    }
}

void Main() {
    int t;
    cin>>t;
    for(int kk=1;kk<=t;kk++) {
        int n;
        cin>>n;
        vector<int>v[n+1];
        for(int i=1;i<=n;i++) {
            int m;
            cin>>m;

            for(int j=0;j<m;j++) {
                int a;
                cin>>a;
                v[i].pb(a);
            }
        }
        // first occurance 
        unordered_map<int,int>foc;


        // store euler path 
        vector<int>euler;
        int count = 0;

        solve(v,1,euler,foc,count);
        int sq = ceil(sqrt(euler.size()));
        vector<int>sqrtd(sq,INT_MAX);
        storeminimum(euler,sq,sqrtd);
        int q;
        cin>>q;
        cout<<"Case "<<kk<<":"<<endl;
        while(q--) {
            int a,b;
            cin>>a>>b;
            int ans = INT_MAX;
            int l=foc[a],r=foc[b];
            while(l<=r) {
                if(l%sq ==0 and l+sq-1<=r) {
                    ans=min(ans,sqrtd[l/sq]);
                    l+=sq;
                } else {
                    ans = min(ans,euler[l]);
                    l++;
                }
            }
            cout<<ans<<endl;
        }
    }
    
}

-71

can some some tell me how can i optmised the little bit so that it may runfaster link


//SATYAM SINGH #include<iostream> #include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define mod 1000000007 #define inf 1e18 #define ump unordered_map #define mp make_pair #define all(v) v.begin(),v.end() #define inpv(v,n) for(int i=0;i<n;i++) cin>>v[i] #define outv(v) for(auto i:v) cout<<i<<' ' #define loop(i,a,b) for(int i=(a);i<(b);i++) #define looprev(i,a,b) for(int i=(a);i>=0;i--) #define log(args...) {string _s = #args ;replace(_s.begin(),_s.end(),',',' ') ; stringstream _ss(_s); istream_iterator<string> _it(_ss) ; err(_it,args);} void err(istream_iterator<string> it) {} template<typename T,typename ... Args> void err(istream_iterator<string> it, T a,Args... args) { cout<<*it<<" = "<<a<<endl; err(++it,args...); } void file_i_o() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void dfs(unordered_map<int,bool>&visited,int src,int &count,vector<int>adj[]) { visited[src] = true; count++; for(auto i:adj[src]) { if(not visited[i]) { dfs(visited,i,count,adj); } } } void solve(vector<int>&comp,vector<int>adj[],int n){ unordered_map<int,bool>visited; for(int i=0;i<n;i++){ int count = 0; if(not visited[i]) { dfs(visited,i,count,adj); comp.pb(count); } } } int main( ) { clock_t begin = clock(); // file_i_o(); // Write your code here.... int n,m; cin>>n>>m; vector<int>v[n]; loop(i,0,m){ int a,b; cin>>a>>b; v[a].pb(b); v[b].pb(a); } vector<int>comp; solve(comp,v,n); ll ans =0; for(int i=0;i<comp.size()-1;i++) { for(int j=i+1;j<comp.size();j++) { ans+=comp[i]*comp[j]; } } cout<<ans<<endl; // code ends here !!!!!! #ifndef ONLINE_JUDGE clock_t end = clock(); // cout<<"\n\nExecuted In: "<<double(end - begin) / CLOCKS_PER_SEC*100<<" seconds"; #endif return 0; }

hey can anyone help me with these problem link -- > link

its giving segmantation fault , so help me

//SATYAM SINGH  
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

#define ll                 long long int
#define pb                 push_back
#define mod                     1000000007
#define inf                     1e18
#define ump                unordered_map
#define mp                 make_pair
#define all(v)                   v.begin(),v.end()

#define  inpv(v,n)  for(int i=0;i<n;i++) cin>>v[i]
#define  outv(v)    for(auto i:v) cout<<i<<' ' 
#define  loop(i,a,b)  for(int i=(a);i<(b);i++)
#define  looprev(i,a,b)  for(int i=(a);i>=0;i--)
#define log(args...)  {string _s = #args ;replace(_s.begin(),_s.end(),',',' ') ; stringstream _ss(_s); istream_iterator<string> _it(_ss) ; err(_it,args);}
void err(istream_iterator<string> it) {}
template<typename T,typename ... Args>
void err(istream_iterator<string> it, T a,Args... args) {
      cout<<*it<<" = "<<a<<endl;
   err(++it,args...);
}

void file_i_o()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}
 vector<pair<ll,ll>>v[1003];


int main( ) {
    clock_t begin = clock();
    file_i_o();
    // Write your code here....
    ll n,m,k;
    cin>>n>>m>>k;
    ll ans = 1ll*n*m;
    map<ll,bool>mp;
    loop(i,0,k) {
        ll a,b,c;
        cin>>a>>b>>c;
        mp[a] = true;
        v[a].pb({b,c});
    }
    ll empty = 0,p=0;

    for(auto i:mp) {
        sort(all(v[i.first]));
    }
    for(auto l:mp) {
        p=0;
        ll i=l.first;

        loop(j,0,v[i].size()) {
            if(v[i][j].first <= p) {
                if(v[i][j].second -p > 0) empty+=v[i][j].second -p;

            } else if(v[i][j].first >p) {
                empty+=v[i][j].second - v[i][j].first + 1;

            }
            p = max(p,v[i][j].second);
        }
    }
    log(ans);
         

    // code ends here !!!!!! 
    #ifndef ONLINE_JUDGE 
      clock_t end = clock();
      cout<<"\n\nExecuted In: "<<double(end - begin) / CLOCKS_PER_SEC*100<<" seconds";
    #endif 
    return 0;
}
On manijuanaCSES Shortest Routes 1, 5 years ago
0

But suppose you get shortest path from another node , so if mark it visited then there may be possibility i missed some small distance then another route

On manijuanaCSES Shortest Routes 1, 5 years ago
0

Why i am getting tle for two test cases below is the code could someone tell me what to do

void solve(vector<node>adj[],int n) {
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>pq;
    vector<ll>dist(n,LLONG_MAX);
    dist[1] = 0;
    pq.push({0,1});

    while(not pq.empty()) {
        ll u =pq.top().second;
        pq.pop();
        
        for(auto i:adj[u]) {
            if(i.des == 1) continue;
            ll v = i.des;
            ll w = i.weight;
            if(dist[u]+w <dist[v]) {
                dist[v] = dist[u] + w;
                pq.push({dist[v],v});
            }
        }
    }

    loop(i,1,n) {
        cout<<dist[i]<<" ";
    }
}

Tnks bro it works , i didnt realised that it is overflowing ,tnk you so much .

Ohk i will try

I am getting wrong ans . could someone tell me where the code make differene ( [yestrday competiton problem .

`](https://mirror.codeforces.com/contest/1541/problem/B).

int main( ) {
    clock_t begin = clock();
    file_i_o();
    // Write your code here....
    int t;
    cin>>t;
    while(t-- ){
        int n;
        cin>>n;
        vector<pair<int,int>>v;
        v.push_back({0,0});
        loop(i,0,n) {
            int x;
            cin>>x;
            v.pb({x,i+1});

        } 
        sort(v.begin()+1,v.end());
        int count =0;
        for(int i=1 ; i<=n;i++) {
            for(ll j=i+1;j<=n;j++) {
                ll left = v[i].first * v[j].first;
                ll right = v[i].second + v[j].second;

                if(left == right) count++;
                if(left > 2*n ) break;
            }
        }
        cout<<count<<endl;
        
    }
     

can someone tell me the concept behind the problem A , i didnt get much how it is possible .