ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1003. Parity

very strange thing :O
Posted by Tbilisi SU: Giorgi , Akaki , [Andrew] 9 Feb 2009 03:44
At last got AC..
my algo is binary search on answer + dfs to check

in my code I had such fragment:
for (i = 0; i < n; i++)
{
    g[i].clear();
        color[i].clear();
    u[i] = -1;
}
and got TLE

then I changed clear() function to resize()
for (i = 0; i < n; i++)
{
    g[i].resize(0);
    color[i].resize(0);
    u[i] = -1;
}
and got AC in 0.2sec
can anybody explain what happend?
clear() works so slow???
Re: very strange thing :O
I'm shocked as well!!! I don't know what place creators of STL wrote it through...

Tbilisi SU: Giorgi, Akaki, [Andrew], thank you for this trick!
Re: very strange thing :O
Posted by Fyodor Menshikov 9 Feb 2009 11:18
What about such explanation?

clear() frees memory, and the next time memory should be allocated again.

resize() just changes internal pointer to the end of array nothing is freed and nothing is allocated then.
Re: very strange thing :O
Posted by Fyodor Menshikov 9 Feb 2009 11:22
Vedernikoff Sergey (HSE: EconomicsForever!) wrote 9 February 2009 03:57
I don't know what place creators of STL wrote it through...

I think that 5x difference is because Timus compiler uses /O1 switch instead of usual /O2. In /O2 mode heap manager is very fast.
Re: very strange thing :O
Posted by Tbilisi SU: Giorgi , Akaki , [Andrew] 9 Feb 2009 14:24
I think it works much slower than 5x . because , number of edges in graph is not more than 10000 , binary search runs log(5000) ~ 12 times . for example if clear() needs O(n) time to clear vector , where n is number of elements in it, then total number of operations is 2*10000*12 = 240000, it seems that it runs 500 times slower...


Edited by author 09.02.2009 14:25