City Tour
Time limit1sMemory limit128 MB
Given a connected 4-regular multigraph with an object on each edge, decide whether some closed Eulerian tour starting at an edge midpoint never lets accumulated interest drop below zero.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Implementation, Math
- Solved
- No attempts yet
Problem
The tourist agency of Byteland is about to launch sightseeing tours along the streets of Bytenburg aboard an open-top bus. Every tour starts and finishes at the agency's centre, and you must decide in the middle of which street that centre will be built.
So that tourists never suspect they missed something interesting, the route of a tour must cover every street of the city. Streets need not be straight and may run through tunnels or viaducts. There are no one-way streets. Each street connects two crossroads, and four streets meet at every crossroad (so every crossroad has degree four). Two crossroads may be joined by more than one street. You may not turn around in the middle of a street, but you may turn around at a crossroad. From any crossroad you can reach any other crossroad through the streets (the graph is connected).
Exactly in the middle of each street there is one object worth seeing (a view, a sculpture, a monument, and so on). The degree of attraction of an object is a non-negative integer. The centre is built next to one such object, that is, in the middle of some street.
During a tour the interest of the tourists changes as follows:
- driving one byte-mile lowers the interest by ;
- seeing an object for the first time raises the interest by that object's degree of attraction;
- at the start of the tour the interest equals the degree of attraction of the object next to the centre.
A route is called attractive if the interest never drops below at any moment of the tour.
Given the city, decide whether an attractive tour route exists.
Input
The first line contains the number of crossroads (). Crossroads are numbered from to , and streets from to .
Each of the next lines describes one street. The -th line describes street with four integers , , , separated by single spaces. and are the crossroads joined by the street (, ). is even and is the length of the street in byte-miles (). is the degree of attraction of the object in the middle of the street ().
Output
Print TAK if an attractive tour route exists, and NIE otherwise. (TAK and NIE mean "yes" and "no" in Polish.)