2833 strange dreams, 2833 strange dreams
DescriptionDescription
Aiden fell into a strange dream: he was trapped in a small house with buttons on the wall and a screen with some information displayed. On the screen, all the buttons must be pressed before they can go out, and some information is provided, indicating that a button can only be pressed after another button is pressed, the buttons that are not mentioned can be pressed at any time. However, Aiden finds that the information on the screen seems to be in conflict. Please help us to determine.
Input description
Input Description
The first line contains N and M, indicating that there are N buttons numbered 1... N, and M messages are displayed on the screen.
The next M line contains two numbers of ai and bi, indicating that the bi button should be pressed after ai. The given information may be duplicated to ensure that ai is equal to bi.
Output description
Output Description
If all buttons can be pressed, "o (∩ _ ∩) o" is output ".
If not, the first line outputs "T_T", and the second line outputs the number of buttons that cannot be confirmed in order due to information conflict. The output does not contain quotation marks.
Sample Input
Sample Input
3 3
1 2
2 3
3 2
Sample output
Sample Output
T_T
2
Data range and prompt
Data Size & Hint
For 30% of the data, ensure that 0 is less than or equal to 100.
For 50% of the data, ensure that 0 is less than or equal to 2000.
For 70% of the data, ensure that 0 is less than or equal to 5000.
For 100% of the data, ensure that 0 <N ≤ limit, 0 <M ≤ 2.5N.
CATEGORY tag
Tags click here to expand
Bare Topology
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<stack> 6 using namespace std; 7 const int MAXN=30001; 8 struct node 9 {10 int u;11 int v;12 int w;13 int next;14 }edge[MAXN];15 int num=1;16 int head[MAXN];17 int rudu[MAXN];18 int tot=0;19 int n,m;20 stack<int>s;21 void topsort()22 {23 for(int i=1;i<=n;i++)24 {25 if(rudu[i]==0)26 {27 s.push(i);28 tot++;29 }30 }31 while(s.size()!=0)32 {33 int p=s.top();34 s.pop();35 for(int i=head[p];i!=-1;i=edge[i].next)36 {37 rudu[edge[i].v]--;38 if(rudu[edge[i].v]==0)39 {40 s.push(edge[i].v);41 tot++;42 }43 }44 }45 if(tot==n)printf("o(∩_∩)o");46 else47 {48 printf("T_T\n%d",n-tot);49 //printf("%d",tot);50 }51 }52 int main()53 {54 55 scanf("%d%d",&n,&m);56 for(int i=1;i<=n;i++)head[i]=-1;57 for(int i=1;i<=m;i++)58 {59 scanf("%d%d",&edge[num].u,&edge[num].v);60 edge[num].next=head[edge[num].u];61 rudu[edge[num].v]++;62 head[edge[num].u]=num++;63 }64 topsort();65 return 0;66 }