I was trying to solve this problem from Egyptian ECPC but I couldn't. Can anyone give me a hint ?

someone who already solved said that he use disjoint set but I can't deal with it using disjoint set .

# | User | Rating |
---|---|---|

1 | tourist | 3496 |

2 | Um_nik | 3337 |

3 | Petr | 3298 |

4 | W4yneb0t | 3218 |

5 | Radewoosh | 3216 |

6 | OO0OOO00O0OOO0O0…O | 3188 |

7 | Syloviaely | 3168 |

8 | izrak | 3109 |

9 | anta | 3106 |

10 | fateice | 3099 |

# | User | Contrib. |
---|---|---|

1 | tourist | 184 |

2 | rng_58 | 170 |

3 | csacademy | 165 |

4 | Petr | 158 |

5 | Swistakk | 154 |

6 | lewin | 149 |

7 | Errichto | 147 |

8 | matthew99 | 146 |

9 | adamant | 141 |

9 | Zlobober | 141 |

I was trying to solve this problem from Egyptian ECPC but I couldn't. Can anyone give me a hint ?

someone who already solved said that he use disjoint set but I can't deal with it using disjoint set .

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Feb/24/2018 22:34:15 (d3).

Desktop version, switch to mobile version.

User lists

Name |
---|

The problem is asking for the maximum spanning tree of the graph where the edge costs between any two numbers in the array is their gcd.

SolutionIterate of all possible gcd's from largest to smallest and consider them your edge costs. At any point you can union all numbers which are divisible by this gcd as long as they weren't connected earlier.

If you process it correctly, it's fast enough.

My solution to help you understand the idea better : https://ideone.com/Vbkhnw

thank you i really appreciate your help