11690 - Money Matters

All about problems in Volume 116. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

Re: 11690 MONEY MATTERS

Post by moxlotus »

brianfry713 wrote:Remove line 72:
count --;
Ahh after u mentioned this, i know where the error is now. just gotta replace it with another variable that i have prepared lol.
Totally overlooked this. you really saved my life~~ :D
lukai
New poster
Posts: 25
Joined: Wed Dec 05, 2012 8:11 pm

Re: 11690 MONEY MATTERS

Post by lukai »

Got TLE

Code: Select all

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
using namespace std;
typedef vector<int>vi;

vi pset(20000);
vector<int>money(20000);
vector<int>total_money(20000);
int numberOfDisjointSet;
void initSet(int N)
{
    numberOfDisjointSet=N;
    pset.assign(N,0);
    total_money.assign(N,0);
    for(int i=0;i<N;i++)
    {
        pset[i]=i;
    }
}

int findSet(int i)
{
    return ((pset[i]==i)? i : pset[i] = findSet(pset[i]));
}

bool isSameSet(int i,int j)
{
    return findSet(i)==findSet(j);
}

void unionSet(int i,int j)
{

    if(!isSameSet(i,j))
    {
        numberOfDisjointSet--;
        pset[findSet(i)]=findSet(j);

    }
}

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

    int cases;
    cin>>cases;
    while(cases--)
    {
        int numOfFrnds,numOfFrndships;
        cin>>numOfFrnds>>numOfFrndships;
        initSet(numOfFrnds);
        while(numOfFrnds--)
        {
            int taka;
            cin>>taka;
            money.push_back(taka);
        }

        while(numOfFrndships--)
        {
            int x,y;
            cin>>x>>y;
            unionSet(x,y);
        }
        set<int>dekhikihoy;

        for(int i=0;i<pset.size();i++)
        {
            dekhikihoy.insert(pset[i]);
        }

        for(int m=0;m<pset.size();m++)
        {
            total_money[pset[m]] += money[m];
        }

        set<int>::iterator it;

        for(it = dekhikihoy.begin();it!=dekhikihoy.end();it++)
        {
                if(total_money[*it]!=0)
                {
                    break;
                }
        }
        if(it!=dekhikihoy.end())
        {
            cout<<"IMPOSSIBLE"<<endl;
        }
        else
        {
            cout<<"POSSIBLE"<<endl;
        }
        pset.clear();
        money.clear();
        total_money.clear();
        dekhikihoy.clear();
    }
	return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11690 MONEY MATTERS

Post by brianfry713 »

Try using scanf and printf instead of cin and cout.
Check input and AC output for thousands of problems on uDebug!
luijhy_9234
New poster
Posts: 4
Joined: Wed Nov 13, 2013 8:47 pm

Re: 11690 MONEY MATTERS

Post by luijhy_9234 »

AC,thanks
Last edited by luijhy_9234 on Thu Apr 03, 2014 12:46 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11690 MONEY MATTERS

Post by brianfry713 »

Change line 19 to:
return (pset[ind] = find_p(pset[ind]));
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 116 (11600-11699)”