{"id":641,"date":"2013-09-16T14:02:21","date_gmt":"2013-09-16T06:02:21","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=641"},"modified":"2013-09-16T14:02:21","modified_gmt":"2013-09-16T06:02:21","slug":"%e4%b9%9d%e5%ba%a6oj-%e9%a2%98%e7%9b%ae1448%ef%bc%9alegal-or-not","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=641","title":{"rendered":"\u4e5d\u5ea6OJ \u9898\u76ee1448\uff1aLegal or Not"},"content":{"rendered":"<dl>\n<dt><b>\u9898\u76ee\u63cf\u8ff0\uff1a<\/b><\/dt>\n<dd>ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many &#8220;holy cows&#8221; like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost &#8220;master&#8221;, and Lost will have a nice &#8220;prentice&#8221;. By and by, there are many pairs of &#8220;master and prentice&#8221;. But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it&#8217;s legal. Nevertheless\uff0csome cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian&#8217;s master and, at the same time, 3xian is HH&#8217;s master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the &#8220;master and prentice&#8221; relation is transitive. It means that if A is B&#8217;s master ans B is C&#8217;s master, then A is C&#8217;s master.\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 &lt;= N, M &lt;= 100). Then M lines follow, each contains a pair of (x, y) which means x is y&#8217;s master and y is x&#8217;s prentice. The input is terminated by N = 0.TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,&#8230;, N-1). We use their numbers instead of their names.\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>For each test case, print in one line the judgement of the messy relationship.If it is legal, output &#8220;YES&#8221;, otherwise &#8220;NO&#8221;.\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>\n<pre>3 2\n0 1\n1 2\n2 2\n0 1\n1 0\n0 0<\/pre>\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>\n<pre>YES\nNO<\/pre>\n<\/dd>\n<\/dl>\n<p>===========================================<br \/>\n\u4e0d\u5408\u6cd5\u60c5\u51b5\u7684\u53d1\u751f\u539f\u56e0\uff1a\u6709\u5411\u56fe\u4e2d\u6709\u5708<br \/>\n\u600e\u4e48\u786e\u5b9a\u6709\u5411\u56fe\u4e2d\u6709\u6ca1\u6709\u5708\uff1a\u62d3\u6251\u6392\u5e8f\uff0c\u6709\u5708\u7684\u6392\u4e0d\u51fa\u7684~<br \/>\n\u600e\u4e48\u8fdb\u884c\u62d3\u6251\u6392\u5e8f\uff1a<br \/>\n\u627e\u5165\u8bfb\u4e3a0\u7684\u8282\u70b9n\uff0c\u5220\u6389\u8282\u70b9\u53ca\u8282\u70b9\u7684\u5173\u8054\u8fb9\uff0c\u7136\u540en\u8282\u70b9\u653e\u5230\u62d3\u6251\u6392\u5e8f\u7684\u961f\u5217\u4e2d<br \/>\n\u5177\u4f53\u8fd8\u662f\u770b\u4ee3\u7801<br \/>\n===========================================<br \/>\n<code lang=\"c++\"><br \/>\n#include <iostream><br \/>\nusing namespace std;<br \/>\nint findZeroInDegree(int **adjMatrix, int N, bool *visited)<br \/>\n{<br \/>\n    \/\/\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u627e\u3010\u5165\u3011\u5ea6\u4e3a0\u7684\u8282\u70b9\u3002<br \/>\n    for(int col=0; col<n; col++)\n    {\n        bool flag = true;\n        for(int row=0; row<n; row++)\n        {\n            if(adjMatrix[row][col] != 0)\n            {\n                flag = false;\n                break;\n            }\n        }\n        if(flag &#038;&#038; !visited[col])\n        {\n            visited[col] = true;\n            return col;\n        }\n    }\n    return -1; \/\/Not found\n}\nvoid removeRelatedEdge(int **adjMatrix, int row, int N)\n{\n    for(int col=0; col<n; col++)\n    {\n        adjMatrix[row][col] = 0;\n    }\n}\nint main()\n{\n    int N;\n    while(cin >> N && N!=0)<br \/>\n    {<br \/>\n        int M;<br \/>\n        cin >> M;<br \/>\n        int **adjMatrix = new int*[N];<br \/>\n        bool *visited = new bool[N];<br \/>\n        int i;<br \/>\n        for(i=0; i<n; i++)\n        {\n            adjMatrix[i] = new int[N];\n            int j;\n            for(j=0; j<n; j++)\n                adjMatrix[i][j] = 0;\n            visited[i] = false;\n        }\n        for(i=0; i<m; i++)\n        {\n            int x, y;\n            cin >> x >> y;<br \/>\n            adjMatrix[x][y] = 1;<br \/>\n        }<br \/>\n        \/\/\/<br \/>\n        int count = 0;<br \/>\n        int nextNode = findZeroInDegree(adjMatrix, N, visited);<br \/>\n        while(nextNode != -1)<br \/>\n        {<br \/>\n            removeRelatedEdge(adjMatrix, nextNode, N);<br \/>\n            nextNode = findZeroInDegree(adjMatrix, N, visited);<br \/>\n            count ++;<br \/>\n        }<br \/>\n        \/\/\/<br \/>\n        if(count == N)<br \/>\n            cout << \"YES\" << endl;\n        else\n            cout << \"NO\" << endl;\n        \/\/\/Delete\n        for(i=0; i<n; i++)\n            delete []adjMatrix[i];\n        delete []adjMatrix;\n        delete []visited;\n    }\n    return 0;\n}\n\/**************************************************************\n    Problem: 1448\n    User: idailylife\n    Language: C++\n    Result: Accepted\n    Time:40 ms\n    Memory:1520 kb\n****************************************************************\/\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0\uff1a ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many &#8220;holy cows&#8221; like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[132],"class_list":["post-641","post","type-post","status-publish","format-standard","hentry","category-study","tag-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=641"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/641\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}