Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

Need help in UVA problem

$
0
0

This is link to my cide -> link to my code

Problem link (UVA 10106)

my code passing all the test cases(cases made by me as well) , but still on submission i am getting WA.

please see if you can figure it out what is wrong with the code , or any test case for which my code fails to print write answer.

thank you in advance.


Wrong answer IPCTRAIN

CHEFTRAF - Editorial

$
0
0

PROBLEM LINK

Practice
Contest

Author:Hanlin Ren
Tester:Jakub Safin
Editorialist:Jakub Safin

DIFFICULTY

HARD

PREREQUISITIES

centroid decomposition, Fenwick trees, preorder sorting, lowest common ancestor

PROBLEM

You're given two trees on the same set of $N$ vertices. Let $d_i(u,v)$ be the distance between vertices $u$ and $v$ in tree number $i$. Compute the sum of $\mathrm{min}(d_1,d_2)$ for all pairs of vertices.

QUICK EXPLANATION

Use centroid decomposition on tree 1, compressing tree 2 when recursing into subtrees. To compute the sum with $u,v$ on different sides from the centroid, use another centroid decomposition on tree 2. To compute sums of the form $\mathrm{min}\left(a(u)+a(v),b(u)+b(v)\right)$, use 3 Fenwick trees.

EXPLANATION

For an $O(N^2)$ solution, it's enough to calculate all distances in both trees by running a DFS from each vertex and computing the answer by brute force. This should be enough to solve the first subtask, but fails miserably on subtasks 2-4.

In subtask 2, we just need the sum of distances in one tree, which is easy: for each edge of length $l$, if removing it splits the tree into subtrees with $n$ and $N-n$ vertices, add $n(N-n)l$ to the answer.

For the 4th subtask, there's an $O(N\log^3 N)$ solution using a well-known tree technique, centroid decomposition - twice. (Using it just once solves the 3rd subtask.)

First of all, let's extend the problem by marking some vertices as dead (in both trees) and computing only $\sum\mathrm{min}(d_1,d_2)$ for pairs of living vertices. Initially, all vertices are living.

Now let's find the centroid $c$ of $T_1$, root $T_1$ at $c$ and compute all distances to the root in $T_1$; let the distance to vertex $v$ be $a(v)$. We need to compute $\sum\min(d_2(u,v),a(u)+a(v))$ for all pairs of living vertices that aren't in the same subtrees. We can do that by first computing the sum for all pairs of living vertices and then subtracting the sums for all subtrees; that can be done in the same way after recursing into subtrees.

Afterwards, we need to compute the sums for all subtrees of $T_1$ created after removing $c$ from it. There's a small problem here: we can't take subtrees after removing $c$ from $T_2$ and can't take full copies of $T_2$ either - that's too slow. For each subtree, we need to generate a smaller tree from $T_2$ that contains all living vertices in it and maybe some more dead vertices so that all paths would remain the same as in $T_2$.

We can sort the living vertices in preorder and take all LCAs of adjacent living vertices to be the set of all additional, possibly dead vertices. This works because of the following lemma:

If $u < v < w$ in the preorder numbering, then $\mathrm{LCA}(u,w)=\mathrm{LCA}(u,v)$ or $\mathrm{LCA}(u,w)=\mathrm{LCA}(v,w)$.

Proof: Denote $\mathrm{LCA}(u,v,w)=l$. Let's prove $l=\mathrm{LCA}(u,w)$. If it doesn't hold, the former is clearly an ancestor of the latter, with $v$ in a different subtree of $l$ than $u,w$ or equal to $l$. In both cases, the subtree containing $u,w$ corresponds to an interval in preorder that doesn't contain $v$, which is a contradiction with $u < v < w$. Now let's prove that $l$ is equal to $\mathrm{LCA}(u,v)$ or $\mathrm{LCA}(v,w)$, by contradiction. If $\mathrm{LCA}(u,v) \neq l$, then $u,v \neq l$ and are in the same subtree of $l$; similarly for $\mathrm{LCA}(v,w) \neq l$. However, $u,v,w$ are then in the same subtree of $l$, which is a contradiction. QED

It turns out that this way, any subtree containing $N$ living vertices only needs to contain at most $N$ dead vertices, everything else is contracted into edges. Therefore, it doesn't affect time complexity.

Now we have vertices, but need to find the edges between them too. If we sort the vertices in preorder again, then we can process them one by one, keep a stack like in a DFS, remove vertices on the stack that aren't ancestors of the current vertex and add an edge between the last vertex in the stack and the current vertex. This is similar to how the preorder numbering of vertices can be done using DFS and takes linear time.

Sum with distances from $c$

We still need to find out how to compute $\sum\min(d_2(u,v),a(u)+a(v))$. Let's use another centroid decomposition, this time on $T_2$. We don't need to split $T_1$ now since it doesn't enter into the sum directly. With a given centroid $c_2$ and distances $b(v)$ to $v$, we need to compute $\sum\min(b(u)+b(v),a(u)+a(v))$. There's no need to worry about excluding pairs of vertices in the same subtrees of $c_2$, we can simply take $u$ to be all vertices in already processed subtrees and sum over all $v$ in the current subtree.

Let's deal with the sum this way: for a given $v$, we're summing up: - $b(u)+b(v)$ over $u$ with $b(u)-a(u) \le a(v)-b(v)$, - $a(u)+a(v)$ over $u$ with $b(u)-a(u) > a(v)-b(v)$.

If there are $n$ such vertices, it's equal to $n b(v) + \sum b(u)$ for the first type and $n a(v) + \sum a(u)$ for the second type. Such prefix/suffix sums can be computed easily with segment trees or Fenwick trees in $O(\log{D})$, where $D$ is the maximum distance in one tree.

However, $D$ can be very large here, so we need to compress all $b(v)-a(v)$ and $a(v)-b(v)$ into a smaller range. The best solution is putting them in an array and sorting; using STL map gives the same complexity, but with a larger constant factor. This way, we only need $O(N)$ memory.

Since processing one subtree with $n$ vertices takes $O(n\log{n})$ and each vertex appears in $O(\log{N})$ vertices, this sum can be computed in $O(N\log^2{N})$. The first centroid decomposition adds another $\log{N}$ to the complexity. Sorting and computing LCAs takes $O(\log{N})$ per vertex, so that's just $O(N\log^2{N})$ in total - the whole algorithm takes $O(N\log^3{N})$ time and $O(N\log{N})$ memory.

AUTHOR'S AND TESTER'S SOLUTIONS

Setter's solution
Tester's solution

DP on Trees - Lecture Series | Tutorial

Subtask 0 meaning

$
0
0

Is subtask 0 in problems same as sample input ?

runtime error while submission

$
0
0

I have written an solution to a problem and compiled it using the online IDE of CodeChef, it shows me that the status is successfully executed but when i submit the same code i am getting an runtime error can someone please help me? Thanks in advance

Solution to CHEFSUM.It is showing W.A.Why?

$
0
0
#include<stdio.h>
int main()
{
int T,i;
scanf("%d",&T);
while(T!=0)
{
int n;
scanf("%d",&n);
int A[n];
for(i=0;i<n;i++)
scanf("%d",&A[i]);
int k=A[0];
int j=0;
for(i=0;i<n;i++)
{
if(A[i]<k)
{
k=A[i];
j=i;
}
}
printf("%d",j+1);
T--;
}
return 0;
}

CIRCLE - Editorial

$
0
0

Problem link : contestpractice

Difficulty : Easy-Medium

Pre-requisites : DP, Flood-fill

Problem : Determine, whether it's possible choose a cell in such a way that all the normal minions would not be trapped (will be free) after the antidote usage at that cell.

Explanation :

At first, let's find all the connected components of normal minions in the given matrix. Every connected component should should contain the maximal number of minions, that is, there should not be any possibility to extend each of the components with a normal minion. Then, if we take any component, all the minions from it will be either free or not. We can use a flood-fill for the components search.

For every component we can find the leftmost and the rightmost column and the topmost and the bottommost row. Having that found, it's easy to determine, whether the correspondent group of minions trapped or not. It it is not trapped, we don't have to do anything in order to rescue them, so we can just ignore this component.

Let's see what happens else. Formally, we need to place a "cross" in the matrix, so we can consider a bounding rectangle of this area (i.e. a connected component of trapped minions). If the cross intersects the bounding rectangle then group will be free after placing the cross with the center in this cell. Otherwise, it will not, so the cell will not be applicable for us.

Consider the bounding rectangle of some connected component. Let its bounding rectangle bottom-left and upper-right coordinates be (X1, Y1) and (X2, Y2). The cross with the center at (X, Y) will not cross this rectangle if at least one of the following conditions will be held:

  • X2< X, Y2< Y. That means that the figure lies completely in the upper-left quadrant relatively (X, Y).
  • X1> X, Y2< Y. That means that the figure lies completely in the upper-right quadrant relatively (X, Y).
  • X2< X, Y1> Y. That means that the figure lies completely in the bottom-left quadrant relatively (X, Y).
  • X1> X, Y1> Y. That means that the figure lies completely in the bottom-right quadrant relatively (X, Y).

We can check all the above statements for all the rectangles at once via prefix sums on a rectangle, that is a pretty popular technique. We can have four prefix-sums arrays for the different cases, that will make the solution easier. So, summing it all up again, we get the following solution:

  • Using flood fill, find all the connected components of the trapped minions.
  • For each of the components find it's bounding rectangle.
  • Mark the corners of the bounding rectangles in their respective rectangle prefix sums.
  • Iterate through all the cells of a matrix and check every cell with the usage of our previously built prefix sums. Each check can be performed in O(1) time, so we get overall complexity of O(N2).

Solutions: SetterTester


KSUBSUM tle issue

FIILMTR what is wrong with my logic

$
0
0

Problem Statement : FILLMTR
My Solution :

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{   public static void main (String[] args) throws java.lang.Exception{
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();

        while(t-- != 0){
            int n = scan.nextInt();
            int q = scan.nextInt();
            int[] arr = new int[n];
            Arrays.fill(arr, -2);
            boolean flag = true;

            for(int i = 0; i < q; i++){
                int x = scan.nextInt() - 1;
                int y = scan.nextInt() - 1;
                int val = scan.nextInt();
                if(!flag)
                    continue;

                if(arr[x] == -2 && arr[y] == -2){
                    if(val == 0){
                        arr[x] = 0;
                        arr[y] = 0;
                    }
                    else{
                        arr[x] = 1;
                        arr[y] = 0;
                    }
                }

                else if(arr[x] == -2){
                    if(val == arr[y]){
                        arr[x] = 0;
                    }
                    else{
                        arr[x] = 1;
                    }
                }

                else if(arr[y] == -2){
                    if(val == arr[x]){
                        arr[y] = 0;
                    }
                    else{
                        arr[y] = 1;
                    }
                }

                else{
                    if(val == 1)
                        flag = arr[x] != arr[y];

                    else
                        flag = arr[x] == arr[y];
                }
            }

            if(flag)
                System.out.println("yes");
            else
                System.out.println("no");

        }
    }
}

I saw the solution using the graph coloring approach but I am not able to understand what is wrong with my logic and the testcases which would give WA for this.

Practice Questions Have Been Removed

$
0
0

Why Are The Practice Questions Done By me Have Been Removed From My Account?

editorial needed for INSQ17E

$
0
0

cannot understand the approach.. would be grateful if someone could help me out in this problem

Please Bring This Feature...!! Why Codechef Dosen't Work On It.

$
0
0

A person's username can not be changed.

But Why So,I think it can be be changed if codechef look into it

This Link Doesn't Work in Codechef

$
0
0

CCDSAP Certification

Performance

September Lunchtime 2017

Practice

These 4 links are just below the picture in the main page of Codechef. In these 4 links in the main page of codechef where picture appears the link of September Lunchtime Shows the same link of CCDSAP Certification

I ma getting RunTime error in CleanUP problem


ACM ICPC...!!! Why Only One Team Is Allowed From Each College

$
0
0

Why Only One Team Is Allowed From Each College in ACM ICPC ?

MULTQ3 help urgent

$
0
0

I have solved this question using segment trees. here is the link to it : https://repl.it/LrK9/0 please help me find the bug. It's running fine on the test case.

How To See Who Has Done The Most Number Of Questions On Codechef?

$
0
0

Can I see users according to the most problems solved?

Announcement: Regarding defamation threads

$
0
0

The members of discuss are reminded that any defamation thread is not allowed at discuss forum strictly. If you have any issues with some member or some report of malpractice, please mail one of the moderators (@vijju123 or @meooow) or mail codechef at their official mails.

If you feel extremely obliged to put up such a thread, please obtain permission from moderator before hand. Failing to do so, will lead to immediate deletion of thread (irrespective of validity of your claims) along with temporary suspension and/or karma penalties.

Defamation thread spoil the mood and atmosphere of discuss, hence must be reported privately to admins.

With Regards@vijju123

this program "small factorials" is giving wrong answer ,can anyone tell me whats wrong in this?

$
0
0

include<iostream>

using namespace std;

int main() { int i,fac=1,a[100],n; cin>>n; for(i=0;i<n;i++) {="" cin="">>a[i]; }

for(i=0;i<n;i++) {="" fac="1;" while(a[i]="">1) { fac=fac*a[i]; a[i]--; } cout<<"\n"<<fac; } return 0; }

Viewing all 39796 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>